Because of the need to provide mail in the Starlight Portal function, so found some asp.net in the internet to send mail articles, but can not meet the demand. Because most of the articles are simple, just tell you how to use MailMessage, and I want to use Gmail mail to send letters, so I will encounter some additional problems, such as: Gmail's SMTP port is not the default 25, but 465 Gmail uses an SSL connection for SMTP
Therefore, to send a message, you must address both of these issues, and other basic issues can easily find a solution. Write down the code here for everyone to use.
MailMessage msg = new MailMessage ();
Msg. from = settings. Systememailaccount;
Msg. to = to;
Msg. Subject = Subject;
Msg. BODY = body;
if (settings. smtpauthenticationrequired)
... {
Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendusername", settings.) Systememailaccount);
Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/sendpassword", settings.) Systememailaccountpassword);
}
if (settings. Smtpport!= 25)
... {
Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpserverport", settings.) Smtpport.tostring ());
}
if (settings. SMTPUSESSL)
... {
Msg. Fields.Add ("Http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");
}
Smtpmail.smtpserver = settings. SmtpServer;
Smtpmail.send (msg);