The. NET Framework sends mail instances

Source: Internet
Author: User
Tags gmail mail mailmessage ssl connection

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.