I had a co-worker recently ask me what I did to send email from a C# web application. I gave him what worked for me. He made a few adjustments and sent it back to me. I did request that he send me what he came up with that finally worked for him. I think he did a better job than I so I am going to post it for you to see. Have a look and see how it may fit into your application or project. Improvements are welcome. He ended up writing a class to call from anywhere in the project. It takes the following arguments; from, to, subject, body, attachments, and isBodyHTML boolean.
using System.Net;
using System.Net.Mail;
public class mailer
{
public Boolean sendemail(String strFrom, string strTo, string strSubject, string strBody, string strAttachmentPath, bool IsBodyHTML)
{
Array arrToArray;
char[] splitter = { ';' };
arrToArray = strTo.Split(splitter);
MailMessage mm = new MailMessage();
mm.From = new MailAddress(strFrom);
mm.Subject = strSubject;
mm.Body = strBody+disclaimer;
mm.IsBodyHtml = IsBodyHTML;
//mm.ReplyTo = new MailAddress("replyto@xyz.com");
foreach (string s in arrToArray)
{
mm.To.Add(new MailAddress(s));
}
if (strAttachmentPath != "")
{
try
{
//Add Attachment
Attachment attachFile = new Attachment(strAttachmentPath);
mm.Attachments.Add(attachFile);
}
catch { }
}
SmtpClient smtp = new SmtpClient();
try
{
smtp.Host = "mail.domain.com";
smtp.Port = 25; //Specify your port No;
smtp.Send(mm);
return true;
}
catch
{
mm.Dispose();
smtp = null;
return false;
}
}
}
Thanks Shane!
Hope this helped someone. Have fun coding and as always, if there are any questions or suggestions, they are welcome. Thank you.