C # Send mail code _ Practical Tips

Source: Internet
Author: User
Tags mail code mailmessage smtpclient
The first code can only send a message without SMTP authentication, but now a lot of email sent need to verify, and then look for the next MSDN Help, found the code to send the verification, posted out hope for everyone to help!
Copy Code code as follows:

public static int SendMail (string to, string body,string subject)
{
Try
{
int ncontain = 0;
Add Sender Address
String from = "Your send email";
MailMessage MAILMSG = new MailMessage ();
Mailmsg.from = new MailAddress (from);
Ncontain + = MailMsg.From.Address.Length;
Add recipient Address
MAILMSG.TO.ADD (to);
Ncontain + = MailMsg.To.ToString (). Length;
Add Message subject
Mailmsg.subject = Subject;
mailmsg.subjectencoding = Encoding.UTF8;
Ncontain + = MailMsg.Subject.Length;
Add message Content
Mailmsg.body = body;
mailmsg.bodyencoding = Encoding.UTF8;
Mailmsg.isbodyhtml = true;
Ncontain + = MailMsg.Body.Length;
if (mailmsg.isbodyhtml = = True)
{
Ncontain + 100;
}
Send mail
Try
{
Define the client that sends the message
SmtpClient client = new SmtpClient ();
Represents authentication with the default credentials of the currently logged-on user
Client. useDefaultCredentials = true;
Include user name and password
Client. Credentials = new System.Net.NetworkCredential (application. Getapplicationsendmail (), application. Getapplicationsendpass ());
Set the IP address of the mail server host
Client. Host = "SMTP server IP"
Set port for mail server
Client. Port = 25;
Configure properties for sending messages
Client. Deliverymethod = Smtpdeliverymethod.network;
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage (strfrom, Strto, Strsubject, strbody);
mailmsg.priority = System.Net.Mail.MailPriority.Normal;
Client. useDefaultCredentials = false;
Send mail
Client. Send (MAILMSG);
return 1;
}
catch (Exception ex) {return 0;}
}
catch (Exception ex) {return 0;}
}
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.