
求C#发送邮件代码
求C#发送邮件测试成功代码,在网上找了好多.因为对机制不太了解所以老是接不到邮件谢谢各位大哥大姐我的QQ:313234782小弟在线等.问题解决加100分...
求C#发送邮件测试成功代码,
在网上找了好多.因为对机制不太了解所以老是接不到邮件
谢谢各位大哥大姐
我的QQ:313234782 小弟在线等.问题解决加100分 展开
在网上找了好多.因为对机制不太了解所以老是接不到邮件
谢谢各位大哥大姐
我的QQ:313234782 小弟在线等.问题解决加100分 展开
3个回答
展开全部
using System.Net.Mail;
/// <summary>
/// 发送邮件程序
/// </summary>
/// <param name="from">发送人邮件地址</param>
/// <param name="fromname">发送人显示名称</param>
/// <param name="to">发送给谁(邮件地址)</param>
/// <param name="subject">标题</param>
/// <param name="body">内容</param>
/// <param name="username">邮件登录名</param>
/// <param name="password">邮件密码</param>
/// <param name="server">邮件服务器</param>
/// <param name="fujian">附件</param>
/// <returns>send ok</returns>
/// 调用方法 SendMail("abc@126.com", "某某人", "cba@126.com", "你好", "我测试下邮件", "邮箱登录名", "邮箱密码", "smtp.126.com", "");
private string SendMail(string from,string fromname,string to,string subject,string body,string username,string password,string server,string fujian)
{
try
{
//邮件发送类
MailMessage mail = new MailMessage();
//是谁发送的邮件
mail.From = new MailAddress(from, fromname);
//发送给谁
mail.To.Add(to);
//标题
mail.Subject = subject;
//内容编码
mail.BodyEncoding = Encoding.Default;
//发送优先级
mail.Priority = MailPriority.High;
//邮件内容
mail.Body = body;
//是否HTML形式发送
mail.IsBodyHtml = true;
//附件
if (fujian.Length > 0)
{
mail.Attachments.Add(new Attachment(fujian));
}
//邮件服务器和端口
SmtpClient smtp = new SmtpClient(server, 25);
smtp.UseDefaultCredentials = true;
//指定发送方式
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//指定登录名和密码
smtp.Credentials = new System.Net.NetworkCredential(username, password);
//超时时间
smtp.Timeout = 10000;
smtp.Send(mail);
return "send ok";
}
catch(Exception exp)
{
return exp.Message;
}
}
/// <summary>
/// 发送邮件程序
/// </summary>
/// <param name="from">发送人邮件地址</param>
/// <param name="fromname">发送人显示名称</param>
/// <param name="to">发送给谁(邮件地址)</param>
/// <param name="subject">标题</param>
/// <param name="body">内容</param>
/// <param name="username">邮件登录名</param>
/// <param name="password">邮件密码</param>
/// <param name="server">邮件服务器</param>
/// <param name="fujian">附件</param>
/// <returns>send ok</returns>
/// 调用方法 SendMail("abc@126.com", "某某人", "cba@126.com", "你好", "我测试下邮件", "邮箱登录名", "邮箱密码", "smtp.126.com", "");
private string SendMail(string from,string fromname,string to,string subject,string body,string username,string password,string server,string fujian)
{
try
{
//邮件发送类
MailMessage mail = new MailMessage();
//是谁发送的邮件
mail.From = new MailAddress(from, fromname);
//发送给谁
mail.To.Add(to);
//标题
mail.Subject = subject;
//内容编码
mail.BodyEncoding = Encoding.Default;
//发送优先级
mail.Priority = MailPriority.High;
//邮件内容
mail.Body = body;
//是否HTML形式发送
mail.IsBodyHtml = true;
//附件
if (fujian.Length > 0)
{
mail.Attachments.Add(new Attachment(fujian));
}
//邮件服务器和端口
SmtpClient smtp = new SmtpClient(server, 25);
smtp.UseDefaultCredentials = true;
//指定发送方式
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//指定登录名和密码
smtp.Credentials = new System.Net.NetworkCredential(username, password);
//超时时间
smtp.Timeout = 10000;
smtp.Send(mail);
return "send ok";
}
catch(Exception exp)
{
return exp.Message;
}
}
展开全部
static void Main(string[] args)
{
CreateMessageWithAttachment(@"服务器抵制");
}
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 =@"E:\补充数据库设计的系统设计\法规备案审查管理子系统-系统设计说明书.doc";
//创建邮件并指定收件人
MailMessage message = new MailMessage("发信人邮箱","收信人邮箱");
message.BodyEncoding = Encoding.Default;
message.Subject = "just a test";
//创建附件
Attachment data = new Attachment(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);
//添加附件
message.Attachments.Add(data);
//发送邮件
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
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);
}
Console.Read();
data.Dispose();
}
{
CreateMessageWithAttachment(@"服务器抵制");
}
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 =@"E:\补充数据库设计的系统设计\法规备案审查管理子系统-系统设计说明书.doc";
//创建邮件并指定收件人
MailMessage message = new MailMessage("发信人邮箱","收信人邮箱");
message.BodyEncoding = Encoding.Default;
message.Subject = "just a test";
//创建附件
Attachment data = new Attachment(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);
//添加附件
message.Attachments.Add(data);
//发送邮件
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
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);
}
Console.Read();
data.Dispose();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
利用jmail组件发送邮件还是可以的,大多数邮箱都能收到!
163、126、sina、sohu、hotmail等,yahoo的不太好!
搜索jmail发送邮件,有很多有效的代码!
163、126、sina、sohu、hotmail等,yahoo的不太好!
搜索jmail发送邮件,有很多有效的代码!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询