[英]Send email from office mail via javamail
我正在通過gmail發送電子郵件,但是當我嘗試從公司郵件發送電子郵件時,卻出現異常。
從公司郵件中,我可以將電子郵件發送給其他員工郵件,但是當我嘗試發送至gmail帳戶時,我收到javax.mail.SendFailedException:無效的地址; 嵌套的異常是:com.sun.mail.smtp.SMTPAddressFailedException:550 5.7.1無法中繼異常。 我該如何解決?
通過gmail
final String username = "mail goes here";
final String password = "password goes here";
final String to = "mail goes here";
final String from = username;
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toGmail));
message.setSubject("Test");
message.setText("Testing");
Transport.send(message);
System.out.println("Mail sent succesfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
通過公司郵件
final String toGmail = "test@gmail.com";
final String toCompany = "one of employees mail goes here";
final String from = "company's noreply mail goes here";
final String to = toGmail;
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.host", "10.100.25.5");
props.setProperty("mail.debug", "true");
// props.put("mail.smtp.auth", "true");
// props.put("mail.smtp.starttls.enable", "true");
// props.put("mail.smtp.ssl.enable", "true");
Session session = Session.getInstance(props);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Test");
message.setText("Testing");
Transport.send(message);
System.out.println("Mail sent succesfully");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
答案在JavaMail FAQ中 。 您不是在對公司郵件服務器進行身份驗證,因此不會讓您在公司外部發送郵件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.