主要要注意的是smtp认证的问题。代码很简单,如下:
调用代码:
namespace EmailTest
{
class Program
{
static void Main(string[] args)
{
try
{
SMTPEmailSender sender = new SMTPEmailSender("mail.longdayinfo.com", "upcodechina@longdayinfo.com", "12345");
sender.From = "upcodechina@longdayinfo.com";
sender.AddReceiver("csfreebird@gmail.com");
sender.Subject = "This is a test";
sender.Content = "hi,beijing team";
sender.AddAttachment("C://aa.jpg");
sender.AddAttachment("C://aaa.csv");
sender.Send();
System.Console.Out.WriteLine("send ok");
}
catch(Exception e)
{
System.Console.Out.WriteLine(e.Source+e.Message);
}
}
}
}
SMTPEmailSender类的源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
namespace Freebird
{
public class SMTPEmailSender
{
//If your smtp server wants authentication,use it
public SMTPEmailSender(String smtpServer,String user,String password)
{
mailMessage = new MailMessage();
smtpClient = new SmtpClient();
smtpClient.Host = smtpServer;
smtpClient.Port = 25;
smtpClient.Credentials = new NetworkCredential(user, password);
}
//If your smtp server doesn't want authentication,use it
public SMTPEmailSender(String smtpServer)
{
mailMessage = new MailMessage();
smtpClient = new SmtpClient(smtpServer);
}
public String Subject
{
get
{
return mailMessage.Subject;
}
set
{
mailMessage.Subject = value;
}
}
//get/set the email's content
public String Content
{
get
{
return mailMessage.Body;
}
set
{
mailMessage.Body = value;
}
}
public String From
{
get
{
return mailMessage.From.Address;
}
set
{
mailMessage.From = new MailAddress(value);
}
}
public void AddReceiver(String email)
{
mailMessage.To.Add(email);
}
public void Send()
{
smtpClient.Send(mailMessage);
}
public void AddAttachment(String filename)
{
mailMessage.Attachments.Add(new Attachment(filename));
}
private MailMessage mailMessage;
private SmtpClient smtpClient;
}
}
C#发送SMTP邮件
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:C#连接access数据库
下一篇:看完C#写点东西
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C#使用QQ邮箱SMTP服务发送邮件
C#使用QQ邮箱SMTP服务发送邮件看,才知道是不是干货
c# 后端 http 经验分享 smtp -
SMTP 邮件发送
python,smtp,邮件发送
邮件发送 smtp python -
C#发送邮件...
关键词:C#发送邮件 发邮件 发送邮件因程序的需要,从网上参考了下别人的代码
System 发送邮件 Network -
C#使用hotmail的SMTP服务器发送邮件
1. 发送者账户为hotmail账号,SMTP服务为: smtp.office365.com。1. 发送者账户为网易
c# 服务器 开发语言 .net 邮件服务器 -
用c#写的smtp邮件发送类
用c#写的smtp邮件发送类
用c#写的smtp邮件发送类 4s 服务器 数据 html