C # send an email (synchronous)

Source: Internet
Author: User
Tags mailmessage smtpclient

[Csharp]
<Span style = "font-size: 14px;" >/// <summary>
/// Send emails synchronously
/// </Summary>
/// <Param name = "fromEmail"> sender's email </param>
/// <Param name = "fromPwd"> sender's email password </param>
/// <Param name = "toEmail"> recipient's email </param>
/// <Param name = "subject"> email title </param>
/// <Param name = "body"> the email is empty. </param>
/// <Param name = "emailType"> mail type </param> smtp.163.com.cn; smtp.qq.com.cn; smtp.126.com.cn; smtp.sina.com.cn
Private void SendEmail (string fromEmail, string fromPwd, string toEmail, string subject, string body, string emailType)
{
MailAddress addrFrom = new MailAddress (fromEmail, fromEmail );
MailAddress addrTo = new MailAddress (toEmail, toEmail );
Attachment att = new Attachment (attFile, MediaTypeNames. Application. Octet );
MailMessage mm = new MailMessage (addrFrom, addrTo );
Mm. BodyEncoding = Encoding. UTF8;
Mm. IsBodyHtml = true;
Mm. Subject = subject;
Mm. Body = body;
 
If (! String. IsNullOrEmpty (attFile ))
{
ContentDisposition cd = att. ContentDisposition;
Cd. CreationDate = File. GetCreationTime (attFile );
Cd. ModificationDate = File. GetLastWriteTime (attFile );
Cd. ReadDate = File. GetLastAccessTime (attFile );
Mm. Attachments. Add (att); // Add an attachment.
}
 
NetworkCredential nc = new NetworkCredential (fromEmail, fromPwd );
SmtpClient smtp = new SmtpClient (emailType );
Smtp. usedefacrecredentials = false;
Smtp. Credentials = nc;
Smtp. EnableSsl = false;
Smtp. DeliveryMethod = SmtpDeliveryMethod. Network;
Smtp. Send (mm );
} </Span>

Related Article

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.