Java 邮件发送代码

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

发送邮件

1、MailOperation.java 主要文件,邮件操作,发送
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main;
 
import java.util.Date;
 
import java.util.Properties;
 
import javax.mail.Message;
 
import javax.mail.Multipart;
 
import javax.mail.Session;
 
import javax.mail.Transport;
 
import javax.mail.internet.InternetAddress;
 
import javax.mail.internet.MimeBodyPart;
 
import javax.mail.internet.MimeMessage;
 
import javax.mail.internet.MimeMultipart;
 
  
 
public class MailOperation {
 
    /**
 
     * @author goming
 
    *TODO:发送邮件
 
    *@param user
 
    *@param password
 
    *@param host
 
    *@param from
 
    *@param to
 
    *@param subject
 
    *@param content
 
    *@return
 
    *@throws Exception
 
     */
 
    public String sendMail(String   user,String password,String host,String from,String to,String subject,String   content) throws Exception {
 
        // TODO Auto-generated method stub
 
       if(to!=null)
 
       {
 
           Properties props=System.getProperties();
 
           props.put("mail.smtp.host", host);
 
           props.put("mail.smtp.auth", "true");
 
  
 
           MailAuthenticator auth=new   MailAuthenticator();
 
           MailAuthenticator.USERNAME=user;
 
           MailAuthenticator.PASSWORD=password;
 
           Session session=Session.getInstance(props,auth);
 
           session.setDebug(true);
 
           try{
 
              MimeMessage message=new   MimeMessage(session);
 
              //message.setDataHandler(new DataHandler(content, "text/html;   charset=utf-8"));//设置邮件内容
 
              message.setFrom(new   InternetAddress(from));
 
              if(!to.trim().equals(""))
 
              message.addRecipient(Message.RecipientType.TO, new   InternetAddress(to.trim()));
 
              message.setSubject(subject);
 
              //  message.setContent(mp)
 
              //message.setText("this is 一个测试邮件");
 
              MimeBodyPart mbp1=new   MimeBodyPart();  //正文
 
              mbp1.setContent(content,"text/html;charset=utf-8");
 
  
 
              Multipart mp=new MimeMultipart();   //整个邮件:正文+附件
 
              mp.addBodyPart(mbp1);
 
              //mp.addBodyPart(mbp2);
 
              message.setContent(mp);
 
              message.setSentDate(new Date());
 
              message.saveChanges();
 
  
 
              Transport   trans=session.getTransport("smtp");
 
              trans.send(message);
 
              System.out.println(message.toString());
 
           }catch(Exception e)
 
           {
 
              e.printStackTrace();
 
              return "failure";
 
           }
 
           return "success";
 
       }
 
       else
 
           return "failure";
 
  
 
    }
 
  
 
    public static void main(String[] args) {
 
       MailOperation operation = new MailOperation();
 
       String user = "admin@xiamengoldenway.com";
 
       String password = "xmG0ldenway";
 
       String host = "smtp.qq.com";
 
       String from = "admin@qq.com";
 
       String to = "******@qq.com";//收件人
 
       String subject = "Test";
 
       String content = "测试邮件";
 
       
 
       try {
 
           String res = operation.sendMail(user,   password, host, from, to, subject, content);
 
           System.out.println(res);
 
       } catch (Exception e) {
 
           // TODO Auto-generated catch block
 
           e.printStackTrace();
 
       }
 
       System.out.println(new Date());
 
    }
 
  
 
}


 

2、MailAuthenticator.jave 封装发件人账号密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import javax.mail.Authenticator;
 
import javax.mail.PasswordAuthentication;
 
  
 
/**
 
 *
 
 *   @author goming
 
 *
 
 */
 
public class MailAuthenticator extends   Authenticator
 
  
 
{
 
         public   static String USERNAME = "";
 
  
 
         public   static String PASSWORD = "";
 
  
 
         public   MailAuthenticator(){}
 
         protected   PasswordAuthentication getPasswordAuthentication(){
 
                   return   new PasswordAuthentication(USERNAME, PASSWORD);
 
         }
 
}


 

3、SendEmail.java 调用类,主要做邮件内容,以及发送一个地址回访!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 
     * 发送邮件
 
     * @author goming
 
     * @param to
 
     * @param uuid
 
     */
 
    @RequestMapping(params = "sendMail")
 
    @ResponseBody
 
    private void sendMail(String   to,HttpServletRequest request){
 
              MailOperation operation = new MailOperation();
 
       //发件人
 
       String user = "*****@qq.com";
 
       String password = "******";
 
       String host = "smtp.qq.com";
 
       String from = "1915960810@qq.com";
 
       String subject = "感谢您注册创意生态资源集成系统软件";
 
       //邮箱内容
 
       String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
 
       StringBuffer sb = new StringBuffer();
 
       Date date=new Date();
 
       Long longtimeLong=date.getTime();
 
       sb.append("<!DOCTYPE>"+"<div bgcolor='#f1fcfa'   style='border:1px solid #d9f4ee; font-size:12px; line-height:22px; color:#005aa0;padding-left:1px;padding-top:5px;   padding-bottom:5px;'><span style='font-weight:bold;'>安全提示:</span>"
 
                      + "<div style='width:950px;font-family:arial;'>为了您的账户安全,我们建议您点击以下链接验证邮箱:<br/><a href='"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"'>"+basePath+"UserRegister.do?ValidateEmail&time="+longtimeLong+"&token="+to+"</a><br/>请在24小时内点击该链接,您也可以将链接复制到浏览器地址栏访问。<br/>本邮件由系统自动发出,请勿回复。<br/>感谢您的使用。<br/>厦门交叉媒体科技有限公司</div>"
 
                     +"</div>");
 
       try {
 
           String res = operation.sendMail(user,   password, host, from, to, subject, sb.toString());
 
//         System.out.println(res);
 
       } catch (Exception e) {
 
           // TODO Auto-generated catch block
 
           e.printStackTrace();
 
       }
 
    }