java 如何从java通过outlook发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3844754/
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
how to send email through outlook from java
提问by M.J.
i have using the following code for sending mail within a domain.
我使用以下代码在域内发送邮件。
public void sendMail(String mailServer, String from, String to,
String subject, String messageBody, String[] attachments)
throws MessagingException, AddressException {
// Setup mail server
Properties props = System.getProperties();
props.put("mail.smtp.host", mailServer);
// Get a mail session
Session session = Session.getDefaultInstance(props, null);
// Define a new mail message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
// Create a message part to represent the body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(messageBody);
// use a MimeMultipart as we need to handle the file attachments
Multipart multipart = new MimeMultipart();
// add the message body to the mime message
multipart.addBodyPart(messageBodyPart);
// add any file attachments to the message
addAtachments(attachments, multipart);
// Put all message parts in the message
message.setContent(multipart);
// Send the message
Transport.send(message);
System.err.println("Message Send");
}
protected void addAtachments(String[] attachments, Multipart multipart)
throws MessagingException, AddressException {
for (int i = 0; i < attachments.length ; i++) {
String filename = attachments[i];
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
// use a JAF FileDataSource as it does MIME type detection
DataSource source = new FileDataSource(filename);
attachmentBodyPart.setDataHandler(new DataHandler(source));
// assume that the filename you want to send is the same as the
// actual file name - could alter this to remove the file path
attachmentBodyPart.setFileName(filename);
// add the attachment
multipart.addBodyPart(attachmentBodyPart);
}
}
but if with the same code i try to send an email outside the domain, say i am sending email from [email protected] to mhsharma@gmail,com then it fails and gives me the following error. 550 5.7.1 Rcpt command failed: Mail denied due to site's policy
但是如果我尝试使用相同的代码在域外发送电子邮件,比如说我从 [email protected] 向 mhsharma@gmail,com 发送电子邮件,那么它就会失败并给我以下错误。 550 5.7.1 Rcpt command failed: Mail denied due to site's policy
Am i missing something in the above code. Please help me
我在上面的代码中遗漏了什么。请帮我
回答by Brian Agnew
I suspect that's not a problem in your code, but in your mail server (sendmail
?) configuration. I would talk to whoever administers your mail infrastructure.
我怀疑这不是您的代码中的问题,而是您的邮件服务器 ( sendmail
?) 配置中的问题。我会与管理您的邮件基础设施的人交谈。
回答by Brad Mace
It's likely that your local mail server requires you to authenticate before sending mail out into the world. This is a common policy to prevent spammers from relaying their mailthrough it.
您的本地邮件服务器很可能要求您在向世界发送邮件之前进行身份验证。这是防止垃圾邮件发送者通过它中继邮件的常用策略。
回答by Isidro.rn
Assuming your machine has Outlook installed... have you tried using moyosoft's java outlook connector? it's pretty simple to use and passes through network restrictions because it connects to your Outlook application and then sends the mail, so any restriction on smtp ports or servers/proxy policies will be ignored if your Outlook client is working fine.
假设您的机器安装了 Outlook……您是否尝试过使用 moyosoft 的 java Outlook 连接器?它使用起来非常简单并且通过网络限制,因为它连接到您的 Outlook 应用程序然后发送邮件,因此如果您的 Outlook 客户端工作正常,则将忽略对 smtp 端口或服务器/代理策略的任何限制。
if you are doing it with command line server-side then i guess this answer is useless for you.
如果你是用命令行服务器端做的,那么我想这个答案对你没用。
Source: i had similar problem (not same error code, more like an intranet restriction) and using this library solved my problem because of the posted above.
来源:我遇到了类似的问题(不是相同的错误代码,更像是内部网限制)并且由于上面发布的内容,使用这个库解决了我的问题。