代码:public static bool SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strto, string strSubject, string strBody, string strFileName)
{
//发送
try
{
//生成一个 使用SMTP发送邮件的客户端对象
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer); //表示以当前登录用户的默认凭据进行身份验证
client.UseDefaultCredentials = true; //包含用户名和密码
client.Credentials = new System.Net.NetworkCredential(UserName, Password); //指定如何发送电子邮件。
//Network 电子邮件通过网络发送到 SMTP 服务器。
//PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。
//SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; //建立邮件对象
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody); //定义邮件正文,主题的编码方式
message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //获取或设置一个值,该值指示电子邮件正文是否为 HTML。
message.IsBodyHtml = true; //指定邮件优先级 message.Priority = System.Net.Mail.MailPriority.Normal; //添加附件
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt");
if (strFileName != "" && strFileName != null)
{
Attachment data = new Attachment(strFileName);
message.Attachments.Add(data);
}
//发件人身份验证,否则163 发不了
client.Credentials = new System.Net.NetworkCredential(strFrom, Password);
client.Send(message);
}
catch (SmtpException ex)
{
throw ex;
}
return true;
}
本地测试正常,能收到邮件,
但是放到服务器上就收不到了。而且不报错谁知道为什么?指点我一下。谢谢了
{
//发送
try
{
//生成一个 使用SMTP发送邮件的客户端对象
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer); //表示以当前登录用户的默认凭据进行身份验证
client.UseDefaultCredentials = true; //包含用户名和密码
client.Credentials = new System.Net.NetworkCredential(UserName, Password); //指定如何发送电子邮件。
//Network 电子邮件通过网络发送到 SMTP 服务器。
//PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。
//SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.PickupDirectoryFromIis; //建立邮件对象
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody); //定义邮件正文,主题的编码方式
message.BodyEncoding = System.Text.Encoding.GetEncoding("UTF-8");
message.SubjectEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //获取或设置一个值,该值指示电子邮件正文是否为 HTML。
message.IsBodyHtml = true; //指定邮件优先级 message.Priority = System.Net.Mail.MailPriority.Normal; //添加附件
//System.Web.Mail.MailAttachment mailAttachment=new System.Web.Mail.MailAttachment(@"f:/baihe.txt");
if (strFileName != "" && strFileName != null)
{
Attachment data = new Attachment(strFileName);
message.Attachments.Add(data);
}
//发件人身份验证,否则163 发不了
client.Credentials = new System.Net.NetworkCredential(strFrom, Password);
client.Send(message);
}
catch (SmtpException ex)
{
throw ex;
}
return true;
}
本地测试正常,能收到邮件,
但是放到服务器上就收不到了。而且不报错谁知道为什么?指点我一下。谢谢了
解决方案 »
- 关于xhEditor 在asp.net 页面内的使用问题,请高手指点。
- gridview需要多个dataset绑定时怎么进行分页?
- <asp:HyperLinkField /> 在dataview中如何使用
- 同时插入两个表,这样写insert语句对吗?
- 三层构架中的Access数据怎么连接?
- 关于ASP.NET2.0用户验证和数据库简单操作的问题
- repeater绑定分类的问题
- 高手请进,看这样的问题,怎么解决??在线……
- 关于使用JavaScript提交表单
- MVC4新建控制器出错
- 急!各位大侠帮帮我这刚入道的新手啊!!在线等各位了~
- 来者有分:ListBox 中拖拽可以改变所显示内容的上下顺序(.net)急急急!!!
用jmail看看
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");
}
}