簡體   English   中英

從Java中的計算機向我的Gmail帳戶發送郵件

[英]Sending a mail to my gmail account from my computer in java

我正在嘗試使用Java代碼將郵件發送到我的gmail。

這是我的代碼:

MailSender.java

    package MailSenderInfo;

    import javax.mail.MessagingException;
    import javax.mail.internet.AddressException;

    public class MailSender {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String from = "mymailId@enmediatech.com";
    String to = "mymailIdto_send@gmail.com";
    String subject = "Test";
    String message = "A test message";

    SendMailBody sendMail = new SendMailBody(from, to, subject, message);

        sendMail.send();

}

}

SenderMailBody.java:

package MailSenderInfo;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
 import javax.mail.Session;
import javax.mail.Transport;
 import javax.mail.internet.AddressException;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class SendMailBody {

private String from;
private String to;
private String subject;
private String text;

public SendMailBody(String from, String to, String subject, String text){
    this.from = from;
    this.to = to;
    this.subject = subject;
    this.text = text;
}

public void send(){

    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");

         props.put("mail.smtp.password", "pass");
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");
    Session mailSession = Session.getDefaultInstance(props);
    Message simpleMessage = new MimeMessage(mailSession);

    InternetAddress fromAddress = null;
    InternetAddress toAddress = null;
    try {
        fromAddress = new InternetAddress(from);
        toAddress = new InternetAddress(to);
    } catch (AddressException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    try {
        simpleMessage.setFrom(fromAddress);
        simpleMessage.setRecipient(RecipientType.TO, toAddress);
        simpleMessage.setSubject(subject);
        simpleMessage.setText(text);

        Transport.send(simpleMessage);          
    } catch (MessagingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

}

這是我得到的錯誤:

 javax.mail.SendFailedException: Sending failed;
 nested exception is:
 class javax.mail.MessagingException: Could not connect to SMTP host:     smtp.gmail.com, port: 25;
       nested exception is:
   java.net.ConnectException: Connection refused: connect
   at javax.mail.Transport.send0(Transport.java:218)
  at javax.mail.Transport.send(Transport.java:80)
  at MailSenderInfo.SendMailBody.send(SendMailBody.java:53)
  at MailSenderInfo.MailSender.main(MailSender.java:20)

在我的系統中,我已經配置了Outlook,並且我的系統中也已經安裝了防病毒軟件。是否由於這兩個原因會發生上述錯誤?任何人都可以指導我。

將gmail smtp端口更改為465或587並添加starttls選項

host = "smtp.gmail.com";    
props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", password);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM