gpt4 book ai didi

java - 如何使用 java api 使用 Outlook 发送电子邮件

转载 作者:行者123 更新时间:2023-12-01 09:27:21 27 4
gpt4 key购买 nike

我正在使用下面的代码使用 java 从 Outlook 发送电子邮件。但出现错误。

代码:

    public static void mail (){
// TODO Auto-generated method stub
//String host="POKCPEX07.corp.absc.local";
String host="POKCPEX07.corp.absc.local";
final String user="satpal.gupta@accenture.com";
String to="satpal.gupta@accenture.com";

//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "false");
props.put("mail.smtp.port", "587");


Session session=Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("SGupta@amerisourcebergen.com","******");
}
});
session.setDebug(true);

try {
MimeMessage message = new MimeMessage(session);
message.saveChanges();
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Test mail");
message.setText("This is test mail.");

//send the message
Transport.send(message);

System.out.println("message sent successfully...");
}
catch (MessagingException e) {e.printStackTrace();}

}
}

错误:

javax.mail.MessagingException: Could not connect to SMTP host: POKCPEX07.corp.absc.local, port: 587;  nested exception is:    java.net.SocketException: Permission denied: connect    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)    at javax.mail.Service.connect(Service.java:258)    at javax.mail.Service.connect(Service.java:137)    at javax.mail.Service.connect(Service.java:86)    at javax.mail.Transport.send0(Transport.java:150)    at javax.mail.Transport.send(Transport.java:80)    at TestEmail.mail(TestEmail.java:50)    at TestEmail.main(TestEmail.java:16)

最佳答案

package com.sendmail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
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 SendAttachmentInEmail {


private static final String SERVIDOR_SMTP = "smtp.office365.com";
private static final int PORTA_SERVIDOR_SMTP = 587;
private static final String CONTA_PADRAO = "xxxx@xxx.com"; //Cofig Mail Id
private static final String SENHA_CONTA_PADRAO = "XYZ"; // Password

private final String from = "xxxx@xxx.com";
private final String to = "xxxx@xxx.com";

private final String subject = "Teste";
private final String messageContent = "Teste de Mensagem";

public void sendEmail() {
final Session session = Session.getInstance(this.getEmailProperties(), new Authenticator() {

@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(CONTA_PADRAO, SENHA_CONTA_PADRAO);
}

});

try {
final Message message = new MimeMessage(session);
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setFrom(new InternetAddress(from));
message.setSubject(subject);
message.setText(messageContent);
message.setSentDate(new Date());
Transport.send(message);
} catch (final MessagingException ex) {
System.out.println(" "+ex);
}
}

public Properties getEmailProperties() {
final Properties config = new Properties();
config.put("mail.smtp.auth", "true");
config.put("mail.smtp.starttls.enable", "true");
config.put("mail.smtp.host", SERVIDOR_SMTP);
config.put("mail.smtp.port", PORTA_SERVIDOR_SMTP);
return config;
}

public static void main(final String[] args) {
new SendAttachmentInEmail().sendEmail();
}

}

关于java - 如何使用 java api 使用 Outlook 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39737829/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com