Java Mail API:通过企业 Outlook 帐户发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20613569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Mail API: send emails via corporate outlook acount
提问by VB_
I want my program to be able to send emails from my corporate outlook account. I looked at many JMA examples the do not seem to be what I want.
我希望我的程序能够从我的公司 Outlook 帐户发送电子邮件。我查看了许多 JMA 示例,但似乎不是我想要的。
- Where can I find simple examples of sending mails via outlook?
- Should I move mailing system to separate service-application? and if so, why?
- 在哪里可以找到通过 Outlook 发送邮件的简单示例?
- 我应该将邮件系统移动到单独的服务应用程序吗?如果是这样,为什么?
采纳答案by adi
All you need is your SMTP settings for your corporate account. Set these in your program using Java mail API and thats it. e.g.
您所需要的只是公司帐户的 SMTP 设置。使用 Java 邮件 API 在您的程序中设置这些,仅此而已。例如
Properties props = System.getProperties();
props.put("mail.smtp.host", "your server here");
Session session = Session.getDefaultInstance(props, null);
回答by Rakshith
You need to download javax.mail
JAR first. Then try the following code:
您需要先下载javax.mail
JAR。然后尝试以下代码:
import java.io.IOException;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public static void main(String[]args) throws IOException {
final String username = "enter your username";
final String password = "enter your password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "outlook.office365.com");
props.put("mail.smtp.port", "587");
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("enter your outlook mail address"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("Enter the recipient mail address"));
message.setSubject("Test");
message.setText("HI");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
回答by Scott Whalen
From what I can see, you are missing the following import:
据我所知,您缺少以下导入:
import java.io.IOException;
Just include it in the class imports at the peginning of this class and your issue should be solved.
只需将它包含在此类的 peginning 类导入中,您的问题就应该得到解决。
回答by Inzimam Tariq IT
I tried with outlook.office365.com
as a host name and got authentication unaccepted exception
. When trying with smtp-mail.outlook.com
I'm able to send mails through outlook with Javamail API
.
我尝试使用outlook.office365.com
作为主机名并得到authentication unaccepted exception
. 尝试使用时,smtp-mail.outlook.com
我可以通过 Outlook 发送邮件Javamail API
。
For further detail Check out the settings of Outlook on outlook official site.
有关更多详细信息,请查看 Outlook官方网站上的 Outlook 设置。
For complete working demo code Read this answer.
有关完整的工作演示代码,请阅读此答案。