using System.Net.Mail; using System.Net.Mime; using System.Net; public static void CreateMessageWithAttachment(string server) { // Specify the file to be attached and sent. // This example assumes that a file named Data.xls exists in the // current working directory. string file = @"D:\asdf.txt"; // Create a message and set up the recipients. MailMessage message = new MailMessage( "[email protected]", "[email protected]", "test", "no du"); // Create the file attachment for this e-mail message. Attachment data = new Attachment(file, MediaTypeNames.Application.Octet); // Add time stamp information for the file. ContentDisposition disposition = data.ContentDisposition; disposition.CreationDate = System.IO.File.GetCreationTime(file); disposition.ModificationDate = System.IO.File.GetLastWriteTime(file); disposition.ReadDate = System.IO.File.GetLastAccessTime(file); // Add the file attachment to this e-mail message. message.Attachments.Add(data); //Send the message. SmtpClient client = new SmtpClient(server); // Add credentials if the SMTP server requires them. //client.Credentials = CredentialCache.DefaultNetworkCredentials; client.UseDefaultCredentials = true; client.Credentials = new System.Net.NetworkCredential("username", "password"); client.Send(message); // Display the values in the ContentDisposition for the attachment. ContentDisposition cd = data.ContentDisposition; Console.WriteLine("Content disposition"); Console.WriteLine(cd.ToString()); Console.WriteLine("File {0}", cd.FileName); Console.WriteLine("Size {0}", cd.Size); Console.WriteLine("Creation {0}", cd.CreationDate); Console.WriteLine("Modification {0}", cd.ModificationDate); Console.WriteLine("Read {0}", cd.ReadDate); Console.WriteLine("Inline {0}", cd.Inline); Console.WriteLine("Parameters: {0}", cd.Parameters.Count); foreach (DictionaryEntry d in cd.Parameters) { Console.WriteLine("{0} = {1}", d.Key, d.Value); } data.Dispose(); } protected void Timer1_Tick(object sender, EventArgs e) { if (DateTime.Now.Second % 20 == 0) { CreateMessageWithAttachment("smtp.163.com"); } }
public static void SendEmail(string from, string to, string cc, string subject, string body, string attachment) { System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to, subject, body); mail.CC.Add(new MailAddress(cc)); mail.Attachments.Add(new Attachment(attachment)); mail.IsBodyHtml = false; System.Net.Mail.SmtpClient send = new System.Net.Mail.SmtpClient("smtp.gmail.com"); send.Port = 587;// gmail; send.EnableSsl = true; send.UseDefaultCredentials = false; send.Credentials = new NetworkCredential("username", "password"); send.Send(mail); }网易的, 要么是你的帐号不支持smtp. 如果支持,仍然发出去收不到就是对方的mail server给当成垃圾邮件了,或者是直接拒收了吧. 检查一下垃圾邮件箱.gmail没问题...
怎么回事,在本地都是正常的,上传服务器以后就会出错呢,这是怎么回事?? Server Error in '/' Application. --------------------------------------------------------------------------------The remote name could not be resolved: 'smtp.163.com' Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Net.WebException: The remote name could not be resolved: 'smtp.163.com'Source Error: Line 63: mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8"); Line 64: mail.Body = MailBody; Line 65: client.Send(mail); Line 66: Line 67: mail = null; Source File: d:\kcch\App_Code\DsyBLL\EmailSend.cs Line: 65 Stack Trace: [WebException: The remote name could not be resolved: 'smtp.163.com'] System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +1447624 System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +190 System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21 System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +318 System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +227 System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +316 System.Net.Mail.SmtpClient.GetConnection() +42 System.Net.Mail.SmtpClient.Send(MailMessage message) +1485[SmtpException: Failure sending mail.] System.Net.Mail.SmtpClient.Send(MailMessage message) +2074 EmailSend.SendReply(String RealName, String QuestionTime, String ServiceNum, String QuestionTypeName, String Question, String UsrEmail, String EmailDisplay) in d:\kcch\App_Code\DsyBLL\EmailSend.cs:65 admin_sendEmail.Button3_Click(Object sender, EventArgs e) in d:\kcch\admin\sendEmail.aspx.cs:118 System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105 System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107 System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11 System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102 -------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 会报上面的错误,怎么解决?
client.Credentials = new System.Net.NetworkCredential("system", "123");
using System.Net.Mime;
using System.Net; public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named Data.xls exists in the
// current working directory.
string file = @"D:\asdf.txt";
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"[email protected]",
"[email protected]",
"test",
"no du"); // Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
//client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential("username", "password");
client.Send(message);
// Display the values in the ContentDisposition for the attachment.
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
data.Dispose();
} protected void Timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Second % 20 == 0)
{
CreateMessageWithAttachment("smtp.163.com");
}
}
关于163邮箱不能发邮件的问题
要注意一下,大概只有05年前注册的支持SMTP和POP服务,以后注册的免费邮箱都不支持了
你可以换其他邮件服务商的,搜狐的好象一直都支持
public static void SendEmail(string from, string to, string cc, string subject, string body, string attachment)
{
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(from, to, subject, body);
mail.CC.Add(new MailAddress(cc));
mail.Attachments.Add(new Attachment(attachment));
mail.IsBodyHtml = false; System.Net.Mail.SmtpClient send = new System.Net.Mail.SmtpClient("smtp.gmail.com");
send.Port = 587;// gmail;
send.EnableSsl = true;
send.UseDefaultCredentials = false;
send.Credentials = new NetworkCredential("username", "password");
send.Send(mail);
}网易的, 要么是你的帐号不支持smtp.
如果支持,仍然发出去收不到就是对方的mail server给当成垃圾邮件了,或者是直接拒收了吧. 检查一下垃圾邮件箱.gmail没问题...
Server Error in '/' Application.
--------------------------------------------------------------------------------The remote name could not be resolved: 'smtp.163.com'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Net.WebException: The remote name could not be resolved: 'smtp.163.com'Source Error:
Line 63: mail.BodyEncoding = System.Text.Encoding.GetEncoding("utf-8");
Line 64: mail.Body = MailBody;
Line 65: client.Send(mail);
Line 66:
Line 67: mail = null;
Source File: d:\kcch\App_Code\DsyBLL\EmailSend.cs Line: 65 Stack Trace:
[WebException: The remote name could not be resolved: 'smtp.163.com']
System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) +1447624
System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) +190
System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) +21
System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) +318
System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) +227
System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) +316
System.Net.Mail.SmtpClient.GetConnection() +42
System.Net.Mail.SmtpClient.Send(MailMessage message) +1485[SmtpException: Failure sending mail.]
System.Net.Mail.SmtpClient.Send(MailMessage message) +2074
EmailSend.SendReply(String RealName, String QuestionTime, String ServiceNum, String QuestionTypeName, String Question, String UsrEmail, String EmailDisplay) in d:\kcch\App_Code\DsyBLL\EmailSend.cs:65
admin_sendEmail.Button3_Click(Object sender, EventArgs e) in d:\kcch\admin\sendEmail.aspx.cs:118
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42 会报上面的错误,怎么解决?