【问题标题】:Send a mail with java and gmail [duplicate]用java和gmail发送邮件[重复]
【发布时间】:2013-09-17 20:39:03
【问题描述】:

我想发送一封带有日历附件 javaxmail 的电子邮件,我创建了这个类:

public void sendEmail(String to, Calendar calendar) {
    try {
        String  d_uname = "myaccount@gmail.com";
        String    d_password = "mypassword";
        String d_host = "smtp.gmail.com";
        String  d_port  = "587";//465,587

        String from = "antonitocea@gmail.com";

        String subject = "Subject";
        String bodyText = "Body";

        Properties prop = new Properties();

        //prop.setProperty("mail.smtp.auth", "true");
        prop.setProperty("mail.smtp.host", "smtp.gmail.com");
        prop.setProperty("mail.smtp.protocol", "smtps");
        prop.setProperty("mail.smtp.starttls.enable", "true");
        prop.setProperty("mail.smtp.ssl.enable", "true");
        prop.setProperty("mail.smtp.port",d_port);
        prop.setProperty("mail.smtp.user", d_uname);


        Session session = Session.getDefaultInstance(prop, null);
        // Define message
        session.setDebug(true);
        MimeMessage message = new MimeMessage(session);
        message.addHeaderLine("method=REQUEST");
        message.addHeaderLine("charset=UTF-8");
        message.addHeaderLine("component=VEVENT");


        message.setFrom(new InternetAddress(from));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        message.setSubject("Outlook Meeting Request Using JavaMail");

        StringBuffer sb = new StringBuffer();

        StringBuffer buffer = sb.append(calendar.toString());

        // Create the message part 
        BodyPart messageBodyPart = new MimeBodyPart();

        // Fill the message 
        messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
        messageBodyPart.setHeader("Content-ID","calendar_message");
        messageBodyPart.setDataHandler(new DataHandler(
                new ByteArrayDataSource(buffer.toString(), "text/calendar")));//very important


        // Create a Multipart 
        Multipart multipart = new MimeMultipart();

        // Add part one 
        multipart.addBodyPart(messageBodyPart);

        Transport t = session.getTransport("smtp");
        t.connect(d_host, 587, d_uname, d_password);


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

        // send message 
        t.send(message);
    } catch (MessagingException me) {
        me.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

看起来没问题,但是,当我尝试发送时,控制台显示此错误:

DEBUG: setDebug: JavaMail version 1.4ea
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 587;
  nested exception is:
    java.net.SocketException: Permission denied: connect

有人可以帮助我吗?提前致谢!!

【问题讨论】:

  • 用户名应该没有'@'和域?

标签: java smtp jakarta-mail


【解决方案1】:

使用下面的代码。

public void sendTemplateEmail() {

        Properties props = new Properties();  
        props.put("mail.smtp.host", "smtp.gmail.com");  
        props.put("mail.smtp.auth", "true");  
        props.put("mail.debug", "true");  
        props.put("mail.smtp.port", 25);  
        props.put("mail.smtp.socketFactory.port", 25);  
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.transport.protocol", "smtp");
        Session mailSession = null;

        mailSession = Session.getInstance(props,  
                new javax.mail.Authenticator() {  
            protected PasswordAuthentication getPasswordAuthentication() {  
                return new PasswordAuthentication("<Gmail User Name>", "<Gmail Password>");  
            }  
        });  


        try {

            Transport transport = mailSession.getTransport();

            MimeMessage message = new MimeMessage(mailSession);

            message.setSubject("Sample Subject");
            message.setFrom(new InternetAddress("Sample@sample.com"));
            String []to = new String[]{"Sample2gmail.com"};
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to[0]));
            String body = "Sample text";
            message.setContent(body,"text/html");
            transport.connect();

            transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
            transport.close();
        } catch (Exception exception) {

        }
    }

替换上面代码中的所有 from 和 to 电子邮件地址以及 and。

【讨论】:

  • 此代码显示与我的代码相同的错误。 “权限被拒绝:连接”。谢谢... 我不明白穿什么。
  • 我测试了代码。它运行良好。您可以发布错误消息吗?对于身份验证,您需要提供您的 gmail 地址和密码。
  • 这是我错误的痕迹。 ## DEBUG: setDebug: JavaMail version 1.4ea DEBUG: getProvider() 返回 javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc] 调试 SMTP: useEhlo true, useAuth true DEBUG SMTP:尝试连接到主机“smtp.gmail.com”,端口 587,isSSL false javax.mail.MessagingException:无法连接到 SMTP 主机:smtp.gmail.com,端口:587;嵌套异常是:java.net.SocketException: Permission denied: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282) at com.sun.mail.smtp.SMTPTransport.protocolCo..
  • 你能测试我的代码是否正确吗?如果是这样,可能是网络问题..
  • 你能把端口从587改成25吗?看起来,587端口没有打开。
猜你喜欢
  • 2018-02-27
  • 2011-08-01
  • 2012-04-13
  • 2012-06-12
  • 2015-01-07
  • 2012-09-11
  • 2011-08-18
  • 1970-01-01
相关资源
最近更新 更多