当前位置:编程学习 > JAVA >>

使用C#和Java发送邮件

 

做用户注册时免不了要让用户使用邮件激活账号,这时就得用到使用程序发邮件的技术了。如下是我在项目中常用的发邮件的方法:

【C#】

 1    usingSystem.Net.Mail;
 2    usingSystem.Net;
 3
 4    publicclassEmailEntity
 5    {
 6        privateMailMessage mm;
 7
 8        ///<summary>
 9        ///发送邮件
10        ///</summary>
11        publicvoidsendMessage()
12        {
13
14            //指定发件人的邮箱地址
15            MailAddress fromAddress = newMailAddress("abc@163.com");
16            //指定收件人的邮箱地址
17            MailAddress toAddress = newMailAddress("efg@qq.com");
18            //创建邮件对象
19            mm = newMailMessage(fromAddress, toAddress);
20            //添加一个密送的邮件地址
21            mm.Bcc.Add(newMailAddress("xiuxiu@qq.com"));
22            //添加一个抄送的邮件地址
23            mm.CC.Add(newMailAddress("me@qq.com"));
24            //指定邮件标题的编码格式
25            mm.SubjectEncoding = Encoding.UTF8;
26            //设置邮件标题
27            mm.Subject = "我是标题";
28            //指定邮件正文编码

29            mm.BodyEncoding = Encoding.UTF8;
30            //指定邮件正文是否text/html类型
31            mm.IsBodyHtml = true;
32            //设置邮件正文内容
33            mm.Body = "<span style='color:#ff0000;font-weight:bold;'>我爱秀秀!</span>";
34            //创建附件
35            Attachment file = newAttachment(AppDomain.CurrentDomain.BaseDirectory+"xiuxiu.jpg");
36            //设置附件的创建日期
37            file.ContentDisposition.CreationDate = DateTime.Now;
38            //设置附件的类型
39            file.ContentType = newSystem.Net.Mime.ContentType("image/jpeg");
40            //将附件添加到邮件中
41            mm.Attachments.Add(file);
42
43            //指定邮件发送服务器的地址和端口号
44            SmtpClient sc = newSmtpClient("smtp.163.com", 25);
45            //指定发件人的身份验证信息
46            sc.Credentials = newNetworkCredential("abc", "123456");
47            //sc.Send(mm);
48            //注册异步发送事件
49            sc.SendCompleted += newSendCompletedEventHandler(sc_SendCompleted);
50            //开始执行异步发送邮件
51            sc.SendAsync(mm, null);
52        }
53        //异步发送邮件完成时处理事件
54        voidsc_SendCompleted(objectsender, System.ComponentModel.AsyncCompletedEventArgs e)
55        {
56            if(mm != null)

57            {
58                mm.Dispose();//释放资源
59            }
60            if(e.Cancelled)
61            {
62                Console.WriteLine("用户已取消!");
63            }
64            elseif(e.Error != null)
65            {
66                Console.WriteLine("发送失败,原因如下:"+ e.Error.Message);
67            }
68            else
69            {
70                Console.WriteLine("发送成功!");
71            }
72        }
73    }

【Java】

  1importjava.util.Date;
  2importjava.util.Properties;
  3importjavax.mail.Authenticator;
  4importjavax.mail.PasswordAuthentication;
  5importjavax.mail.Message.RecipientType;
  6importjavax.mail.MessagingException;
  7importjavax.mail.Session;
  8importjavax.mail.Transport;
  9importjavax.mail.internet.AddressException;
 10importjavax.mail.internet.InternetAddress;
 11importjavax.mail.internet.MimeMessage;
12

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,