gpt4 book ai didi

java - 如何直接从Java代码发送带有附件的私有(private)公司电子邮件?

转载 作者:行者123 更新时间:2023-11-30 04:12:18 24 4
gpt4 key购买 nike

我很好奇是否有任何方法可以直接从 Java 代码发送带有附件的电子邮件。该电子邮件属于公司 mylogin@companyxxx.com`,即使用 Java 操作 Outlook?或者也许我不需要操作Outlook,有登录名和密码等人员就足够了...

最佳答案

是的,您可以使用 javamail 来做到这一点,您可以从here下载jar 。对于 Outlook,您只需按如下方式设置属性:

    Properties props = new Properties();
props.put("mail.smtp.user", username);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.socketFactory.port", "587");

或者如果您想要完整的示例,请使用以下方法:

    public int sendMailWithAttachment(String to, String subject, String body, String filepath, String sendFileName) {
final String username = "YOUR EMAIL";
final String password = "YOUR PWD";
Properties props = new Properties();
props.put("mail.smtp.user", username);
props.put("mail.smtp.host", "smtp.live.com");
props.put("mail.smtp.port", "25");
props.put("mail.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.EnableSSL.enable", "true");
props.setProperty("mail.smtp.port", "587");
props.setProperty("mail.smtp.socketFactory.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(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setText(body);
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
BodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent("<html><body>HELLO</body></html>", "text/html");
DataSource source = new FileDataSource(filepath);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(sendFileName);
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(htmlPart);
message.setContent(multipart);
Transport.send(message);
return 1;
} catch (Exception e) {
return 0;
}
}

关于java - 如何直接从Java代码发送带有附件的私有(private)公司电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19311366/

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