最近学习邮件这块,在网上找了Jmail.NET 感觉挺不错的,拿回来试用.
免费版的Jmail .NET 1.1 不包含POP3组件.只能测试SMTP了.
新建项目,添加 Dimac.JMail.dll 和 Dimac.JMail.Smtp.dll 引用,
先看下官网的例子:
1
using
System;
2 using Dimac.JMail;
3
4 namespace JMailTest
5 {
6 class MainClass
7 {
8 [STAThread]
9 static void Main( string [] args )
10 {
11 // create JMail message
12 Message message = new Message();
13
14 // set sender
15 message.From.Email = " me@mydomain.com " ;
16
17 // add a recipient
18 message.To.Add( " myfriend@hisdomain.com " );
19
20 // set the subject & body
21 message.Subject = " Hello, world! " ;
22 message.BodyText = " Hello, world! My hovercraft is full of eels. " ;
23
24 // add an attachment
25 message.Attachments.Add( " C:\\me_dancing.wmv " );
26
27 // send the message
28 try
29 {
30 Smtp.Send( message, " mail.mydomain.com " );
31
32 Console.WriteLine( " The message has been sent. " );
33 }
34 catch ( Exception ex )
35 {
36 Console.WriteLine( " Failed to send message: {0} " , ex.Message );
37 }
38 }
39 }
40 }
2 using Dimac.JMail;
3
4 namespace JMailTest
5 {
6 class MainClass
7 {
8 [STAThread]
9 static void Main( string [] args )
10 {
11 // create JMail message
12 Message message = new Message();
13
14 // set sender
15 message.From.Email = " me@mydomain.com " ;
16
17 // add a recipient
18 message.To.Add( " myfriend@hisdomain.com " );
19
20 // set the subject & body
21 message.Subject = " Hello, world! " ;
22 message.BodyText = " Hello, world! My hovercraft is full of eels. " ;
23
24 // add an attachment
25 message.Attachments.Add( " C:\\me_dancing.wmv " );
26
27 // send the message
28 try
29 {
30 Smtp.Send( message, " mail.mydomain.com " );
31
32 Console.WriteLine( " The message has been sent. " );
33 }
34 catch ( Exception ex )
35 {
36 Console.WriteLine( " Failed to send message: {0} " , ex.Message );
37 }
38 }
39 }
40 }
用这个做测试,失败!
拿安装包里面的例子逐个进行测试,都是出现这个问题,最后看文档,应该之前版本的code,1.1版本不在支持.
根据资料进行分析,最后终于能成功发送,代码如下:
1
private
void
btnSend_Click(
object
sender, EventArgs e)
2 {
3 Dimac.JMail.Message msg = new Dimac.JMail.Message();
4 // 发件人
5 msg.From.Email = txtFrom.Text;
6 // 显示姓名
7 msg.From.FullName = txtName.Text;
8 // 收件人
9 msg.To.Add( new Address(txtTo.Text));
10 // 邮件主题
11 msg.Subject = txtSub.Text;
12 // 邮件内容
13 msg.BodyText = rtxtCont.Text;
14 // 字符集.这里设置为GB2312 不设置收到邮件之后中文显示乱码
15 msg.Charset = Encoding.GetEncoding( " GB2312 " );
16 // 附件
17 msg.Attachments.Add(@txtAttach.Text);
18
19 try
20 {
21 // 设置SMTP
22 Smtp smt = new Smtp();
23 smt.UserName = txtUserName.Text; // your username
24 smt.Password = txtPwd.Text; // your password
25 smt.HostName = txtHostName.Text; // SMTP服务器
26 smt.Domain = txtDomain.Text; // 主机名
27 smt.Port = short .Parse(txtPort.Text); // 端口
28 smt.Authentication = SmtpAuthentication.Login; // !!验证方式
29 smt.Send(msg);
30 MessageBox.Show( " 邮件发送成功 " , " 提示 " );
31 }
32 catch (Exception ex)
33 {
34 MessageBox.Show(ex.Message);
35 }
36 }
2 {
3 Dimac.JMail.Message msg = new Dimac.JMail.Message();
4 // 发件人
5 msg.From.Email = txtFrom.Text;
6 // 显示姓名
7 msg.From.FullName = txtName.Text;
8 // 收件人
9 msg.To.Add( new Address(txtTo.Text));
10 // 邮件主题
11 msg.Subject = txtSub.Text;
12 // 邮件内容
13 msg.BodyText = rtxtCont.Text;
14 // 字符集.这里设置为GB2312 不设置收到邮件之后中文显示乱码
15 msg.Charset = Encoding.GetEncoding( " GB2312 " );
16 // 附件
17 msg.Attachments.Add(@txtAttach.Text);
18
19 try
20 {
21 // 设置SMTP
22 Smtp smt = new Smtp();
23 smt.UserName = txtUserName.Text; // your username
24 smt.Password = txtPwd.Text; // your password
25 smt.HostName = txtHostName.Text; // SMTP服务器
26 smt.Domain = txtDomain.Text; // 主机名
27 smt.Port = short .Parse(txtPort.Text); // 端口
28 smt.Authentication = SmtpAuthentication.Login; // !!验证方式
29 smt.Send(msg);
30 MessageBox.Show( " 邮件发送成功 " , " 提示 " );
31 }
32 catch (Exception ex)
33 {
34 MessageBox.Show(ex.Message);
35 }
36 }
测试: