C#:
- MailAddress from = new MailAddress("xcf@192.168.1.152", "张三");
- MailAddress to = new MailAddress("xcf0631@gmail.com", "李四");
- MailMessage message = new MailMessage(from, to);
- message.Subject = "使用SmtpClient类发送邮件";
- message.Body = @"这是一封测试邮件,通过.net发送。";
- //MailAddress copy = new MailAddress("xcf0631@gmail.com");
- //message.CC.Add(copy);
- SmtpClient client = new SmtpClient("localhost");
- client.Credentials = CredentialCache.DefaultNetworkCredentials;
- string info = String.Format("SMTP主机:{0},给{1}发送一封e-mail邮件。",
- client.Host, to.Address);
- System.Diagnostics.Debug.Write(info);
- client.Send(message);
结果:
本地邮件服务器:
带附件邮件:
- SmtpClient client = new SmtpClient("localhost", 25);
- //client.Credentials =new NetworkCredential("xcf@192.168.1.152", "123456");
- client.Credentials = CredentialCache.DefaultNetworkCredentials;
- using (MailMessage msg = new MailMessage())
- {
- msg.From = new MailAddress("xcf@192.168.1.152");
- msg.Subject = "测试";
- msg.Body = "这是一封测试邮件.";
- msg.Attachments.Add(new Attachment("c:\\test.txt", "text/plain"));
- msg.Attachments.Add(new Attachment("C:\\QTTask.exe", "application/octet-stream"));
- msg.To.Add(new MailAddress("xcf-007@163.com"));
- client.Send(msg);
- }
本文转自 xcf007 51CTO博客,原文链接:http://blog.51cto.com/xcf007/479715,如需转载请自行联系原作者