[英]how to send email through outlook from java
我已經使用以下代碼在域內發送郵件。
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);
}
}
但是,如果我嘗試使用相同的代碼嘗試在域外發送電子郵件,例如我將郵件從mjsharma@domain.com發送到mhsharma @ gmail,com,則失敗,並給我以下錯誤。 550 5.7.1 Rcpt command failed: Mail denied due to site's policy
我在上面的代碼中缺少什么嗎? 請幫我
我懷疑這不是您的代碼中的問題,而是您的郵件服務器( sendmail
?)配置中的問題。 我將與管理您的郵件基礎結構的任何人交談。
假設您的計算機已安裝Outlook,是否嘗試過使用moyosoft的java Outlook Connector? 它非常簡單易用,並且通過了網絡限制,因為它連接到Outlook應用程序然后發送郵件,因此,如果Outlook客戶端運行良好,則對smtp端口或服務器/代理策略的任何限制都將被忽略。
如果您正在使用命令行服務器端進行操作,那么我猜這個答案對您毫無用處。
資料來源:我有類似的問題(不是相同的錯誤代碼,更像是Intranet限制),由於上面的發布,使用此庫解決了我的問題。
您的本地郵件服務器可能需要您進行身份驗證,然后才能向世界發送郵件。 這是防止垃圾郵件發送者通過其轉發郵件的常用策略。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.