Email sending in. NET 2.0

Source: Internet
Author: User
Tags mailmessage smtpclient

I wrote a mail sending class:

Public class sendmail
{
Private Static string m_username;
Private Static string m_password;
Private Static string m_hostname;

Static string returnmsg = "";

Public static void newmailserver (string user, string PWD, string host ){
M_username = user;
M_password = PWD;
M_hostname = host;
}

Public static string send (string from, string sentto, string CC, string
BC, string subject, string body)
{
Try
{
Mailmessage message = new mailmessage (New mailaddress (from, "Rui" +
(Char) 0xd8 + "Chen", system. Text. encoding. utf8), new mailaddress (sentto ));

Message. Subject = subject;
Message. Body = body;
Message. isbodyhtml = true;

Smtpclient mailclient = new smtpclient ();

Mailclient. Host = m_hostname;
Mailclient. Credentials = new networkcredential (m_username, m_password );
Mailclient. usedefacrecredentials = false;
Mailclient. Timeout = 100000;
Mailclient. deliverymethod = smtpdeliverymethod. Network;

Mailclient. Send (Message );
Return returnmsg;
}
Catch (system. net. Mail. smtpexception sex)
{
Return sex. statuscode. tostring () + ":" + sex. Source + sex. tostring ();
}
}

Private Sendmail ()
{

}
}

However, an error occurs during the call:

Mailboxnamenotallowed: System system. net. Mail. smtpexception: mailbox name
Not allowed. The server response was: You are not authorized to send mail
>, Authentication is required
System. net. Mail. mailcommand. checkresponse (smtpstatuscode statuscode, string
Response) at system. net. Mail. mailcommand. Send (smtpconnection Conn, byte []
Command, string from) at system. net. Mail. smtptransport. Sendmail (mailaddress
From, mailaddresscollection recipients, smtpfailedrecipientexception &
Exception) at system. net. Mail. smtpclient. Send (mailmessage message)
Merlion. Web. Mail. Sendmail. Send (string from, string sentto, string CC, string
BC, string subject, string body) in C:/Documents ents and settings/Chen Rui/My
Documents/Visual Studio 2005/websites/website3/app_code/sendmail. CS: Line 53

8. Why? After an afternoon, I was depressed. You can't simply use socket to write your own SMTP implementation.

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.