使用javamail API发送带有附件的电子邮件 [英] Sending an email with an attachment using javamail API

查看:450
本文介绍了使用javamail API发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我发送没有附件的电子邮件时,我收到电子邮件,但是当我添加附件我没有收到任何内容,我没有收到任何错误信息。



这是我使用的代码:

  public void send()throws AddressException,MessagingException {
//系统属性

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


/ *
*创建一些属性并获取默认会话
* /
session = Session.getDefaultInstance(props,null);

// session
会话session = Session.getInstance(道具,null);

消息消息= 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(你的名字);
writer.append(',');

writer.append('\\\
');
writer.append(邹);
writer.append(',');
writer.append(26);
writer.append(',');
writer.append(zouhaier);


//生成任何你想要的数据

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

如何更正?

解决方案


  1. 禁用您的防病毒

因为你有这样的警告





尝试此代码...它帮助您....

  public class SendMail {
public SendMail()throws MessagingException {
String host =smtp.gmail.com;
String Password =............;
String from =XXXXXXXXXX@gmail.com;
String toAddress =YYYYYYYYYYYYYY@gmail.com;
String filename =C:/SendAttachment.java;
//获取系统属性
属性props = System.getProperties();
props.put(mail.smtp.host,主机);
props.put(mail.smtps.auth,true);
props.put(mail.smtp.starttls.enable,true);
会话session = Session.getInstance(props,null);

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));

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

message.setSubject(JavaMail附件);

BodyPart messageBodyPart = new MimeBodyPart();

messageBodyPart.setText(这里是文件);

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 {
传输tr = session.getTransport(smtps);
tr.connect(host,from,Password);
tr.sendMessage(message,message.getAllRecipients());
System.out.println(Mail Sent Successful);
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);
}
}
}


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?

解决方案

  1. Disable Your Anti Virus

because you have some warning like this

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

这篇关于使用javamail API发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆