如何在发送电子邮件时写出身体部位?

时间:2013-09-13 09:06:00

标签: javax.mail

在脚本完成后发送电子邮件时,我无法添加任何正文部分。我想添加身体,

你好,xyz,

请查看随附的报告。

最诚挚的问候,

ABC

我使用以下代码发送电子邮件。任何人都可以指导我如何发送电子邮件。

package util;



//set CLASSPATH=%CLASSPATH%;activation.jar;mail.jar

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;



public class SendMail

{
    public static void zipAndSendReport()

    {

        TestUtil.zip(System.getProperty("user.dir") +"\\HtmlReports");


                String[] to={"xyz@xyz.com"};

                String[] cc={"xyzff@xyz.com","xyzaa@xyz.com"};
                String[] bcc={"xyzrwer@xyz.com"};

                //This is for google

                SendMail.sendMail("acv.ddg@gmail.com",
                                    "070122dasdad3183",
                                    "smtp.gmail.com",
                                    "465",
                                    "true",
                                    "true",
                                     true,
                                    "javax.net.ssl.SSLSocketFactory",
                                    "false",
                                    to,
                                    cc,
                                    bcc,
                                    "Automation test Reports",
                                    "Please find the reports attached.\n\n Regards\nWebMaster",
                                    System.getProperty("user.dir")+"\\Reports.zip",
                                    "Reports.zip");


    }



        public  static boolean sendMail(String userName,
                String passWord,
                String host,
                String port,
                String starttls,
                String auth,
                boolean debug,
                String socketFactoryClass,
                String fallback,
                String[] to,
                String[] cc,
                String[] bcc,
                String subject,
                String text,
                String attachmentPath,
                String attachmentName){


                Properties props = new Properties();

                //Properties props=System.getProperties();

        props.put("mail.smtp.user", userName);

        props.put("mail.smtp.host", host);

                if(!"".equals(port))

        props.put("mail.smtp.port", port);

                if(!"".equals(starttls))

        props.put("mail.smtp.starttls.enable",starttls);

        props.put("mail.smtp.auth", auth);
       // props.put("mail.smtps.auth", "true");


                if(debug){

                props.put("mail.smtp.debug", "true");

                }else{

                props.put("mail.smtp.debug", "false");         

                }

                if(!"".equals(port))

        props.put("mail.smtp.socketFactory.port", port);

                if(!"".equals(socketFactoryClass))

        props.put("mail.smtp.socketFactory.class",socketFactoryClass);

                if(!"".equals(fallback))

        props.put("mail.smtp.socketFactory.fallback", fallback);



        try

        {

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

            Session session = Session.getDefaultInstance(props,
                    new javax.mail.Authenticator() 
          {
               protected PasswordAuthentication getPasswordAuthentication()
               { return new PasswordAuthentication("acv.ddg@gmail.com","070122sd3183");     }
          });

            session.setDebug(debug);

            MimeMessage msg = new MimeMessage(session);

            msg.setText(text);

            msg.setSubject(subject);
            //attachment start
            // create the message part 

            Multipart multipart = new MimeMultipart();
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            DataSource source = 
              new FileDataSource(attachmentPath);
            messageBodyPart.setDataHandler(
              new DataHandler(source));
            messageBodyPart.setFileName(attachmentName);
            multipart.addBodyPart(messageBodyPart);

            // attachment ends

            // Put parts in message
            msg.setContent(multipart);
            msg.setFrom(new InternetAddress(userName));

                        for(int i=0;i<to.length;i++){

            msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to[i]));

                        }

                        for(int i=0;i<cc.length;i++){

            msg.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));

                        }

                        for(int i=0;i<bcc.length;i++){

            msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));

                        }

            msg.saveChanges();

                        Transport transport = session.getTransport("smtp");

                        transport.connect(host, userName, passWord);

                        transport.sendMessage(msg, msg.getAllRecipients());

                        transport.close();

                        return true;

        }

        catch (Exception mex)

        {

            mex.printStackTrace();

                        return false;

        }

        }



}

请告诉我们此代码是否有效或有更好的方法吗?

谢谢,

Sudhansu

1 个答案:

答案 0 :(得分:0)

您的MimeMessage可以通过setText()或多部分正文设置。

由于你的使用过程,你必须使用multipart。要在邮件中添加一些纯文本,请创建另一个MimeBodyPart并将其添加为Multipart的第一个:

....
Multipart multipart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text);
mmp.addBodyPart(textPart);
MimeBodyPart messageBodyPart = new MimeBodyPart();
....