smtp.gmail.com没有在java上的ubuntu服务器上工作

时间:2015-03-23 20:03:42

标签: java smtp javamail

我正面临通过smtp.gmail.com发送邮件的问题。它完全适用于我的本地系统,但它不能在服务器上运行。即使它没有产生任何类型的错误,所以我可以解决它们。

当我尝试在服务器上发送电子邮件时,它不能在服务器上运行,但它完全适用于我的本地系统。我不知道实际问题是什么。

这是我的邮件脚本: -

package src;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import net.sf.jasperreports.engine.JRException;
import source.JdbcHelper1;
/**
 *
 * @author user2
 */
public class MailEnquery {
    JdbcHelper1 jdbc;
    Connection con;


        public String  MailToVendor(String[] flt_venid,String enq_id,String[] vender_name, String[] email,String air_craft,String item_show,String EnquiryReference) throws FileNotFoundException, JRException {
        PreparedStatement pst = null;
        String status = null;
        Exception ex = null;
        String url=null;
        String username = "xxxxxx";
        String to = "xxxx";
        String from = "xxxxx";
        String host = "smtp.gmail.com";
        String filename = null;

        String subject = "ENQUIRY # "+EnquiryReference;
        String pass = "xxxx";
        String port = "587";
        File attachment = null;
        String orignalname = null;

        try {
                for(int j=0;j<flt_venid.length&&j<email.length;j++){             
               url="xxxx";

            Properties props = new Properties();
            props.put("mail.smtp.user", username);
            props.put("mail.smtp.host", host);
            if (!"".equals(port)) {
                props.put("mail.smtp.port", port);
            }
            if (!"".equals("true")) {
                props.put("mail.smtp.starttls.enable", "true");
            }
            props.put("mail.smtp.auth", "true");
            if (true) {
                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("javax.net.ssl.SSLSocketFactory")) {
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            }
            if (!"".equals("false")) {
                props.put("mail.smtp.socketFactory.fallback", "false");
            } 
            // Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, null);
            session.setDebug(true);
            MimeMessage msg = new MimeMessage(session); // create a message
            msg.setFrom(new InternetAddress(from));
            msg.setSubject(subject);
            MimeBodyPart mbp1 = new MimeBodyPart();// create and fill the first message part
          //  mbp1.setText(msgText1);
            MimeBodyPart mbp2 = new MimeBodyPart();// create the second message part
            Multipart mp = new MimeMultipart(); // create the Multipart and add its parts to it
            mp.addBodyPart(mbp1);
            if (attachment != null) {
                File filename1 = attachment;
                FileDataSource fds = new FileDataSource(filename1);
                mbp2.setDataHandler(new DataHandler(fds));
                mbp2.setFileName(fds.getName());
                // mbp2.setFileName(filename);
                mp.addBodyPart(mbp2);
            }
            msg.setContent(mp);// add the Multipart to the message
            msg.setSentDate(new Date()); // set the Date: header

            String html = "content";
        //HTMLDataSource is an inner class
        msg.setDataHandler(new DataHandler(new HTMLDataSource(html)));

            Transport transport = session.getTransport("smtps");
            transport.connect(host, username, pass);
            try {
                 String tos = email[j];
              //  InternetAddress[] address ;//= {new InternetAddress(to)};
              // for (int i = 0; i < tos.length; i++) {
                   // if (tos[i].trim().equalsIgnoreCase("")) {
                   // } else {//
                       InternetAddress[] address= {new InternetAddress(tos)};
                        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(tos));
                        msg.setRecipients(Message.RecipientType.TO, address);
               msg.setRecipients(Message.RecipientType.CC, "xxxx");
               msg.setRecipients(Message.RecipientType.BCC, "xxxx");

                msg.saveChanges(); // don't forget this
                transport.sendMessage(msg, msg.getAllRecipients());
                status="OK";
           // }
             //  }
            } finally {
                transport.close();
            }
                }
        } catch (MessagingException mex) {
            mex.printStackTrace();
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }
            status=null;
        }finally{
            System.out.println("the status in mail sending file is=="+status);
            return status;
        }
    }


        static class HTMLDataSource implements DataSource {
        private String html;

        public HTMLDataSource(String htmlString) {
            html = htmlString;
        }

        // Return html string in an InputStream.
        // A new stream must be returned each time.
        public InputStream getInputStream() throws IOException {
            if (html == null) throw new IOException("Null HTML");
            return new ByteArrayInputStream(html.getBytes());
        }

        public OutputStream getOutputStream() throws IOException {
            throw new IOException("This DataHandler cannot write HTML");
        }

        public String getContentType() {
            return "text/html";
        }

        public String getName() {
            return "JAF text/html dataSource to send e-mail only";
        }
    }
}

0 个答案:

没有答案