javamail发送邮件

 EmailInfoDto emailDto = (EmailInfoDto)JsonUtil.getDTO(JsonUtil.getJSONString(data), EmailInfoDto.class);

String[] addresses =emailDto.getAddress().split(";");
List<String> add=new ArrayList<String>();
String userEmail=emailDto.getEmailName().substring(0,emailDto.getEmailName().indexOf("@"));
for(String address : addresses){
add.add(address.substring(address.indexOf("<")+1, address.indexOf(">")));
}
String emailType=emailDto.getEmailName().substring(emailDto.getEmailName().indexOf("@")+1,emailDto.getEmailName().indexOf("."));
String smtpType=null;
if(emailType.equals("163")){
smtpType="smtp.163.com";
}else if(emailType.equals("qq")){
smtpType="smtp.qq.com";
}else if(emailType.equals("sina")){
smtpType="smtp://smtp.sina.com.cn";
}else if(emailType.equals("yahoo")){
smtpType="smtp.mail.yahoo.com.cn";
}else if(emailType.equals("gmail")){
smtpType="smtp.gmail.cn";
}
String address="";
if(add.size()>0){
for(int i=0;i<add.size();i++){
address=address+","+add.get(i);
}
Properties props = new Properties();
props.setProperty("mail.smtp.auth", "true");
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", smtpType);
FesPasswordAuthentication fesPasswordAuthentication =new FesPasswordAuthentication(userEmail,emailDto.getEmailPwd());
Session session = Session.getInstance(props,fesPasswordAuthentication);
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailDto.getEmailName()));
message.setSubject(emailDto.getTitle());
message.setRecipients(RecipientType.TO, 
InternetAddress.parse(address));
message.setContent(emailDto.getContent(), "text/html;charset=gbk");
Transport.send(message);

你可能感兴趣的:(邮件,javamail,发送邮件)