The following c# function will let you send email using Google SMTP Server. All you need to do is just replace network credentials with your Google user name and password.
//Start Send Email Function
public string SendMail(string toList, string from, string ccList, string subject, string body)
{
MailMessage message = new MailMessage();
SmtpClient smtpClient = new SmtpClient();
string msg = string.Empty;
try
{
MailAddress fromAddress = new MailAddress(from);
message.From = fromAddress;
message.To.Add(toList);
if (ccList != null && ccList != string.Empty)
message.CC.Add(ccList);
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
smtpClient.Host = "smtp.gmail.com"; // We use gmail as our smtp client
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = true;
smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "YourGmailPassword");
smtpClient.Send(message);
msg = "Email sent Successfully.";
}
catch (Exception ex)
{
msg = ex.Message;
}
return msg;
}
//End Send Email Function
Note: Before providing credentials, you need to set
UserDefaultCredentials to
false.
And you can make a call to the above function by using:
Response.Write(SendMail(recipient Address, "[email protected]", "ccList if any", "subject", "body"))
I have tested this code and used it to send mails.
You can even attach Files to the email, will update my post soon.. Cheers