使用javax.mail发送邮件示例

rsl1atfo  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(179)

我下载了以下示例:
https://app.box.com/shared/etj0lts287r5lc1hqeex
当我直接执行jar文件时,它工作得很好。但是当使用Netbeans 7.0运行项目时,它抛出一个错误:

error: could not connect to SMTP host:smtp.gmail.com,port:465

有什么问题吗?
这是我的代码按钮动作执行私有void发送按钮动作执行(java.awt.event.ActionEvent evt){

boolean isSent = true;

    try {

    Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", txtfrom.getText());
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", txthostName.getText());    
    properties.put("mail.smtp.user", txtfrom.getText());
    properties.put("mail.smtp.port", txtPort.getText());
    properties.put("mail.smtp.socketFactory.port", txtPort.getText());
    properties.put("mail.smtp.starttls.enable","true");
    properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    properties.put("mail.smtp.socketFactory.fallback", "false");

    Authenticator mailAuthenticator = new MailAuthenticator();
Session mailSession = Session.getDefaultInstance(properties,mailAuthenticator);
    Message message = new MimeMessage(mailSession);

    Transport transport = mailSession.getTransport("smtps");
    transport.connect("smtp.gmail.com",465,"abc@gmail.com","12345");

    InternetAddress fromAddress = new InternetAddress(txtfrom.getText());
    InternetAddress toAddress = new InternetAddress(txtto.getText());

    message.setFrom(fromAddress);
    message.setRecipient(RecipientType.TO, toAddress);

    message.setSubject(txtsubject.getText());
    message.setText(txtcontent.getText());

    Transport.send(message);

    } catch (Exception e) {
        lblInfo.setText("ERROR:" + e.getMessage());            
        isSent = false;
    }

    if(isSent == true) {
        sendButton.setEnabled(false);
        lblInfo.setText("Your e-mail has been sent."); 
    } 

}
q9rjltbz

q9rjltbz1#

请考虑对您的代码进行以下更改,
//使用“smtps”:)

transport = mailSession.getTransport("smtps");

//端口465用于身份验证,并通过Gmail解决,Yahoo主机transport.connect("smtp.gmail.com",465,"myid","password");

5cnsuln7

5cnsuln72#

JavaMail是发送电子邮件的传统答案。请注意port和其他配置是如何设置的。
这里有一个例子来做这件事。

package example.java.mail;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class SendFromGmail {
    public static void main(String args[]){
        try{
            String host = "smtp.gmail.com";
            String from = "example@gmail.com";
            String pass = "mypassword123";
            Properties props = System.getProperties();
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", host);
            props.put("mail.smtp.user", from);
            props.put("mail.smtp.password", pass);
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.auth", "true");

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

            Session session = Session.getDefaultInstance(props, null);
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            InternetAddress[] toAddress = new InternetAddress[to.length];

            // To get the array of addresses
            for( int i=0; i < to.length; i++ ) { // changed from a while loop
                toAddress[i] = new InternetAddress(to[i]);
            }
            System.out.println(Message.RecipientType.TO);

            for( int i=0; i < toAddress.length; i++) { // changed from a while loop
                message.addRecipient(Message.RecipientType.TO, toAddress[i]);
            }
            message.setSubject("sending in a group");
            message.setText("Welcome to JavaMail");
            Transport transport = session.getTransport("smtp");
            transport.connect(host, from, pass);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
        }
        catch(Exception e){
            e.getMessage();
        }
    }
}

查看更多Java mail sending示例。

相关问题