C# code to send email without attachment

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 184
The below code will do so

public static void SendEmailWithoutAttachment(Tuple<string, int, string, string> smptTuple, Tuple<string, string, string, string, string> emailInfoTuple)
{
try
{
string smptHost = smptTuple.Item1;
int smptPort = smptTuple.Item2;
string smptUser = smptTuple.Item3;
string smptPassword = smptTuple.Item4;
string fromEmailAddress = emailInfoTuple.Item1;
string toEmailAddress = emailInfoTuple.Item2;
string ccEmailAddress = emailInfoTuple.Item3;
string subject = emailInfoTuple.Item4;
string body = emailInfoTuple.Item5;


MailMessage mailMsg = new MailMessage();
MailAddress emailAddress = new MailAddress(toEmailAddress);
mailMsg.To.Add(emailAddress);

foreach (var address in ccEmailAddress.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries))
{
mailMsg.CC.Add(address);
}

mailMsg.From = new MailAddress(fromEmailAddress);

mailMsg.Subject = subject;
mailMsg.Body = body;
mailMsg.IsBodyHtml = true;

// Init SmtpClient
SmtpClient smtpClient = new SmtpClient(smptHost, smptPort);
smtpClient.EnableSsl = false;
System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(smptUser, smptPassword);
smtpClient.Credentials = credentials;

//send email
smtpClient.Send(mailMsg);

}
catch (Exception ex)
{
throw ex;
}
}

Comments or Responses

Login to post response