繁体   English   中英

使用JavaMail API通过Servlet通过附件发送的电子邮件未通过

[英]Email sent with attachment via servlet using JavaMail API not getting through

我使用以下代码通过JavaMail API发送电子邮件

可以有人指导我如何在此代码中添加附件,以便我可以发送带有附件的电子邮件。

package Javamail;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.*;

import java.io.*;

public class SendmailUsejavamail
{

  private static final String SMTP_HOST_NAME = "smtp.gmail.com";

  private static final String SMTP_AUTH_USER = "aaaa@gmail.com";

  private static final String SMTP_AUTH_PWD  = "xxxxxxxxx";


  private static final String emailMsgTxt      = "Online Order Confirmation Message. Also include the Tracking Number.";

  private static final String emailSubjectTxt  = "Order Confirmation Subject";

  private static final String emailFromAddress = "bbbbb@gmail.com";

  // Add List of Email address to who email needs to be sent to

  private static final String[] emailList = {"abc@gmail.com", "abcd@gmail.com"};


  public static void main(String args[]) throws Exception{

    SendmailUsejavamail smtpMailSender = new SendmailUsejavamail();

    smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);

    System.out.println("Sucessfully Sent mail to All Users");

  }


  public void postMail( String recipients[ ], String subject,
                            String message , String from) throws MessagingException
  {

    boolean debug = false;

     //Set the host smtp address
     Properties props = new Properties();

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

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

    Authenticator auth = new SMTPAuthenticator();

    Session session = Session.getDefaultInstance(props, auth);

    session.setDebug(debug);

    // create a message
    Message msg = new MimeMessage(session);

    // set the from and to address
    InternetAddress addressFrom = new InternetAddress(from);
    msg.setFrom(addressFrom);
    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++)
    {
        addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    // Setting the Subject and Content Type
    msg.setSubject(subject);
    msg.setContent(message, "text/plain");
    Transport.send(msg);
 }

/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/

private class SMTPAuthenticator extends javax.mail.Authenticator{

    public PasswordAuthentication getPasswordAuthentication(){

        String username = SMTP_AUTH_USER;

        String password = SMTP_AUTH_PWD;

        return new PasswordAuthentication(username, password);

    }

}

}
 MimeMessage message = new MimeMessage(session);

 // create the message part 
  MimeBodyPart messageBodyPart = new MimeBodyPart();

 //fill message
  messageBodyPart.setText("Hi");

   Multipart multipart = new MimeMultipart();
   multipart.addBodyPart(messageBodyPart);

// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =  new FileDataSource(fileAttachment); // your file
messageBodyPart.setDataHandler( new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

// Send the message
Transport.send( message );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM