您的位置:首页 > 其它

.net实现发送邮件,同步方式

2010-11-03 11:15 721 查看
using System;
using System.Net;
using System.Text;
using System.Net.Mail;

namespace ConsoleApplication1
{
class Program
{

public static void Main(string[] args)
{
// Command line argument must the the SMTP host.
SmtpClient client = new SmtpClient();

//这里选择自己的邮箱服务地址,如果发送失败参见解决办法

client.Host = "smtp.sohu.com";//主机 地址
client.Port = 25;//端口号

Console.WriteLine("请输入您的搜狐邮箱:");
string frommail = Console.ReadLine();

Console.WriteLine("请输入您邮箱的密码:");
string pwd = Console.ReadLine();

Console.WriteLine("请输入您要显示的昵称:");
string  sname = Console.ReadLine();

Console.WriteLine("请输入收件人的邮箱:");
string tomail = Console.ReadLine();
MailAddress from = new MailAddress(frommail, sname, Encoding.UTF8);//发件人,第二个参数为显示的发件人昵称

MailAddress to = new MailAddress(tomail);//收件人
client.DeliveryMethod = SmtpDeliveryMethod.Network;//以网络smtp方式发送
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(frommail, pwd);//发件方的用户名和密码

// Specify the message content.
MailMessage message = new MailMessage(from, to);

Console.WriteLine("请输入邮件正文");
message.Body = Console.ReadLine();

message.BodyEncoding = Encoding.UTF8;//邮件正文编码

Console.WriteLine("请输入邮件主题");
message.Subject = Console.ReadLine();

message.SubjectEncoding = Encoding.UTF8;

client.Send(message);

Console.WriteLine("sent successfully");
message.Dispose();
Console.WriteLine("Goodbye.");
Console.ReadLine();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: