Need code for sending mail

Posted by Rrana under ASP.NET on 10/10/2011 | Points: 10 | Views : 1648 | Status : [Member] | Replies : 4
Hi,

I need code for sending a mail through C#.net and also i need web.config code for this mail.


Thanks in advance

Sucharitha Goud
Bank Of America..



Responses

Posted by: Blessyjees on: 10/10/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Try this
 public static void SendMail(string fromAddress,string toAddress,string subject,string content,string host,int port,string userName,string password)

{


using (MailMessage mailMessage = new MailMessage(fromAddress, toAddress, subject, content))
{
mailMessage.IsBodyHtml = true;
SmtpClient mailClient = new SmtpClient();
mailClient.Host = host;
if (port != 0)
{
mailClient.Port = port;
}
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
if (!string.IsNullOrEmpty(userName))
mailClient.Credentials = new NetworkCredential(userName, password);
mailClient.Send(mailMessage);

}

}


Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Gausi on: 10/10/2011 [Member] Starter | Points: 25

Up
0
Down
 public class EmailSender

{
public string From = string.Empty;
public string To = string.Empty;
public string User = string.Empty;
public string Password = string.Empty;
public string Subject = string.Empty;
public string Body = string.Empty;
public List<string> AttachmentPaths;
public string Host = string.Empty;
public int Port;
public string CC = string.Empty;
public bool IsHtml = false;
public int SendUsing = 0;//0 = Network, 1 = PickupDirectory, 2 = SpecifiedPickupDirectory
public bool UseSSL = true;
public int AuthenticationMode = 1;//0 = No authentication, 1 = Plain Text, 2 = NTLM authentication
public string ErrorMessage = string.Empty;
public EmailSender()
{

}

public void SendEmail()
{
new Thread(new ThreadStart(SendMessage)).Start();
}
/// <summary>
/// Send Email Method
/// </summary>
public void SendMessage()
{
try
{
MailMessage oMessage = new MailMessage();
SmtpClient smtpClient = new SmtpClient(Host);

oMessage.From = new MailAddress(From);
oMessage.To.Add(To);
oMessage.Subject = Subject;
oMessage.IsBodyHtml = IsHtml;
oMessage.Body = Body;

if (CC != string.Empty)
oMessage.CC.Add(CC);

switch (SendUsing)
{
case 0:
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
break;
case 1:
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
break;
case 2:
smtpClient.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
break;
default:
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
break;

}
if (AuthenticationMode > 0)
{
smtpClient.Credentials = new NetworkCredential(User, Password);
}

smtpClient.Port = Port;
smtpClient.EnableSsl = UseSSL;

string AttachZipPath = new FileInfo(AttachmentPaths[0]).Directory.ToString() + "//" + Subject + " " + DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss") + ".zip";

// Create and add the attachment



if (ZipFile.WriteZipFile(AttachmentPaths, AttachZipPath, 8))
{
AttachmentPaths.ForEach(i =>
{
if (i != string.Empty)
{
File.Delete(i);
}
});
}

oMessage.Attachments.Add(new Attachment(AttachZipPath));

try
{
smtpClient.Send(oMessage);
}
catch (Exception ex)
{
ErrorMessage = ex.ToString();

}
oMessage.Dispose();
File.Delete(AttachZipPath);
}
catch (Exception ex)
{
ErrorMessage = ex.ToString();
}
}

}


Regards,

Gaurav Singh
(gausi)

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ketvin on: 10/11/2011 [Member] Starter | Points: 25

Up
0
Down
hi,

AspEmail is a classic ASP component, and AspEmail.NET is a native .NET component, that enable your Web application to send email messages via any external SMTP server. Basic mail-sending functionality these products offer is absolutely free.

In addition to the free basic functions, AspEmail & AspEmail.NET offer a number of premium features including secure mail, message queuing, Unicode support, embedded images, TLS, authentication, and others.



Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: perfectchourasia-9163 on: 11/17/2011 [Member] Starter | Points: 25

Up
0
Down
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
string StrMailBody = "Name: " + txtname.Text.ToString().Trim() + ", Contact No.: " + txtcont.Text.ToString().Trim();
SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
client.Host = "smtp.gmail.com";
client.Port = 587;

// setup Smtp authentication
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("emailid", "pwd");
client.UseDefaultCredentials = false;
client.Credentials = credentials;

MailMessage msg = new MailMessage();
msg.From = new MailAddress("emailid");
msg.To.Add(new MailAddress("receiver emaid id "));
//msg.CC.Add(new MailAddress("email"));
msg.Bcc.Add(new MailAddress("sandeepchrs@yahoo.com"));

msg.Subject = "SMWS - Contact Us";
msg.IsBodyHtml = true;
msg.Body = string.Format("<html><head></head><body><b>Name: </b>" + txtname.Text.ToString().Trim() + "<b>Contact us: </b>" + txtcont.Text.ToString().Trim() + "</body>");

try
{
client.Send(msg);
//lblMsg.Text = "Your message has been successfully sent.";
}
catch (Exception ex)
{
//lblMsg.ForeColor = Color.Red;
//lblMsg.Text = "Error occured while sending your message." + ex.Message;
}
}
}


http://sandeep-chourasia.blogspot.com/2011/05/send-mail-in-aspnet.html

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

Rrana, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response