-1

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

嗨,xyz,

请在附件中找到所附的报告。

最诚挚的问候,

美国广播公司。

我正在使用以下代码发送电子邮件。谁能指导我如何发送电子邮件。

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;

        }

        }



}

请让我们知道此代码是否有效,或者有更好的方法吗?

谢谢,

苏丹苏

4

1 回答 1

0

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

由于您使用附件,因此您必须使用多部分。要将一些纯文本添加到消息中,请创建另一个MimeBodyPart并将其作为第一个添加到您的Multipart

....
Multipart multipart = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(text);
mmp.addBodyPart(textPart);
MimeBodyPart messageBodyPart = new MimeBodyPart();
....
于 2013-09-13T09:31:20.967 回答