gpt4 book ai didi

java - 使用 javamail API 发送带附件的电子邮件

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:05:01 24 4
gpt4 key购买 nike

我正在尝试用 Java 发送带有附件文件的电子邮件。

当我发送不带附件的电子邮件时,我会收到电子邮件,但当我添加附件时,我不会收到任何东西,也不会收到任何错误消息。

这是我使用的代码:

public void send () throws AddressException, MessagingException{
//system properties

Properties props = new Properties();
props.put("mail.smtp.localhost", "localhost");
props.put("mail.smtp.host",Configurations.getInstance().email_serverIp);


/*
* create some properties and get the default Session
*/
session = Session.getDefaultInstance(props, null);

//session
Session session = Session.getInstance(props, null);

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("zouhaier.mhamdi@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("zouhaier.mhamdi@gmail.com"));
message.setSubject("Testing Subject");
message.setText("PFA");

MimeBodyPart messageBodyPart = new MimeBodyPart();

Multipart multipart = new MimeMultipart();
generateCsvFile("/tmp/test.csv");
messageBodyPart = new MimeBodyPart();
String file = "/tmp/test.csv";
String fileName = "test.csv";
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

System.out.println("Sending");

Transport.send(message);

System.out.println("Done");

}

private static void generateCsvFile(String sFileName)
{
try
{

FileWriter writer = new FileWriter(sFileName);

writer.append("DisplayName");
writer.append(',');
writer.append("Age");
writer.append(',');
writer.append("YOUR NAME");
writer.append(',');

writer.append('\n');
writer.append("Zou");
writer.append(',');
writer.append("26");
writer.append(',');
writer.append("zouhaier");


//generate whatever data you want

writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}

我该如何纠正这个问题?

最佳答案

  1. 禁用您的防病毒软件

因为你有这样的警告

warning message form antivirus

试试这个代码……它可以帮助你……

public class SendMail {
public SendMail() throws MessagingException {
String host = "smtp.gmail.com";
String Password = "............";
String from = "XXXXXXXXXX@gmail.com";
String toAddress = "YYYYYYYYYYYYY@gmail.com";
String filename = "C:/SendAttachment.java";
// Get system properties
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtps.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

message.setRecipients(Message.RecipientType.TO, toAddress);

message.setSubject("JavaMail Attachment");

BodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText("Here's the file");

Multipart multipart = new MimeMultipart();

multipart.addBodyPart(messageBodyPart);

messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName(filename);

multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

try {
Transport tr = session.getTransport("smtps");
tr.connect(host, from, Password);
tr.sendMessage(message, message.getAllRecipients());
System.out.println("Mail Sent Successfully");
tr.close();

} catch (SendFailedException sfe) {

System.out.println(sfe);
}
}
public static void main(String args[]){
try {
SendMail sm = new SendMail();
} catch (MessagingException ex) {
Logger.getLogger(SendMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

关于java - 使用 javamail API 发送带附件的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17156544/

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