java发送邮件程序带附件,使用javamail API发送带有附件的电子邮件

I am trying to send an email with an attachment file in Java.

When I send the email without an attachment I receive the email, but when I add the attachment I don't receive anything and I don't get any error messages.

This is the code I am using:

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();

}

}

How can I correct this?

解决方案

Disable Your Anti Virus

because you have some warning like this

LYu34.jpg

Try this code... It Help You....

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);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值