写了一个发送邮件的类:
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.UseDefaultCredentials = 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()
{
}
}
但是在调用的时候出现错误:
MailboxNameNotAllowed : System System.Net.Mail.SmtpException: Mailbox name
not allowed. The server response was: You are not authorized to send mail as
>, authentication is required at
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) at
Merlion.Web.Mail.SendMail.Send(String from, String sentto, String cc, String
bc, String subject, String body) in c:/Documents and Settings/陈锐/My
Documents/Visual Studio 2005/WebSites/WebSite3/App_Code/SendMail.cs:line 53
8直到是什么原因,搞了一下午,郁闷。不行干脆用Socket写一个自己的SMTP实现算了。