Heim > Fragen und Antworten > Hauptteil
sendMail(String receiver,String content){
Properties properties=new Properties();
properties.setProperty("mail.smtp.auth","true");
properties.setProperty("mail.transport.protocol","smtp");
properties.setProperty("mail.host","smtp.163.com");
Session session =Session.getInstance(properties,new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("*********163.com","*******");
}
}
);
Message message=new MimeMessage(session);
try {
message.setFrom(new InternetAddress("*********”@163.com"));
} catch (AddressException e) {
return "邮箱的登录失败,请检查你的账户密码是否正确!";
} catch (MessagingException e) {
return "你的账户异常,请重新发送!";
}
try {
message.addRecipients(RecipientType.TO,InternetAddress.parse(receiver));
message.setSubject("MachineEye");
message.setText(content);
} catch (AddressException e1) {
return "收件人账户信息不正确!";
} catch (MessagingException e1) {
return "收件人账户异常!";
}
try {
Transport.send(message);
} catch (MessagingException e) {
return "邮件发送失败";
}
return "发送成功!";
}
迷茫2017-04-18 10:50:04
首先,跟语言没有关系。
1..要声明邮件地址和头信息(msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); 披上outlook的马甲)
3.特殊情况,就是可能有些服务器在你已经披上合法的马甲后还是会当作垃圾邮箱处理,如网易,那是因为你的邮件内容可能存在一些,敏感的字眼,或者违法的信息导致的。