//设置发件人
String sendFrom = "test1@cesoo.com";
try{
//获得发送实体
SMTPTransport t = (SMTPTransport)session.getTransport(ssl ? "smtps" : "smtp");
//与SMTP主机相连
t.connect("192.168.1.100","test1","123456");
MimeMessage msg = new MimeMessage(session);
//设置邮件各个字段
String subject = "testing mail subject";
String personalName = "LR JAVA Vuser";
String body = "testing mail body";
String charset = "utf-8";
Inte.netAddress[] to = new InternetAddress[1];
InternetAddress from = new
InternetAddress(sendFrom,personalName,charset);
to[0] =new InternetAddress(sendTo,personalName,charset);
msg.setFrom(from);
msg.setRecipients(Message.RecipientType.TO,to);
msg.setSubject(subject,charset);
msg.setText(body,charset);
msg.setSentDate(new Date());
//发送邮件
t.sendMessage(msg, msg.getAllRecipients());
}
catch (Exception e){
e.printStackTrace();
}
return 0;
}//end of action
以上代码运行成功后,Outlook即可收到一封邮件,如图8-19所示。
文章来源于领测软件测试网 https://www.ltesting.net/