尝试使用Gmail发送电子邮件时出错

时间:2014-03-23 19:28:35

标签: java email

我正在使用“gmail”在需要时发送电子邮件 -

import com.sun.xml.internal.org.jvnet.mimepull.MIMEMessage;

import java.io.BufferedReader;
import java.io.FileReader;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
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;

public class CityGridJobStatusCheckAndEmail {

    Properties emailProperties;
    Session mailSession;
    MIMEMessage emailMessage;

    public static void main(String[] args) throws AddressException, MessagingException {

        String fileName = "C:/Users/****/Downloads/";
        Start.txt;
        ";;
        // String fileName=args[0];
        try {
            //Create object of FileReader
            FileReader inputFile = new FileReader(fileName);

            //Instantiate the BufferedReader Class
            BufferedReader bufferReader = new BufferedReader(inputFile);

            //Variable to hold the one line data
            String line;


            // Read file line by line and print on the console

            while ((line = bufferReader.readLine()) != null) {


                if (line.equals("Start of program")) {

                    CityGridJobStatusCheckAndEmail javaEmail = new CityGridJobStatusCheckAndEmail();

                    javaEmail.setMailServerProperties();
                    javaEmail.createEmailMessageforstartofprogram(line);
                    javaEmail.sendEmailifJobCompleted();
                }
            }

            //Close the buffer reader
            bufferReader.close();
        } catch (Exception e) {
            System.out.println("Error while reading file line by line:" + e.getMessage());
        }


    }

    public void setMailServerProperties() {

        String emailPort = "587"; //gmail's smtp port//'465' gmail smtp port, test it with that as well,587//

        emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", emailPort);
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");

    }

    public void createEmailMessageforstartofprogram(String line) throws AddressException, MessagingException {
        String[] toEmails = { "****@gmail.com" };
        String emailSubject = "Java Email";
        String emailBody = "CitySearch : The Bulk Update program has started";

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        for (int i = 0; i < toEmails.length; i++) {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }

        emailMessage.setSubject(emailSubject);
        emailMessage.setContent(emailBody, "text/html"); //for a html email
        //emailMessage.setText(emailBody);// for a text email

    }

    public void createEmailMessage(String jobstatus, String jobId) throws AddressException, MessagingException {
        String[] toEmails = { "*****@gmail.com" }; //techops@pasadenalabs.com
        String emailSubject = "Java Email";
        String emailBody = "The Bulk Update with ID :" + jobId + " , and status :" + jobstatus;

        mailSession = Session.getDefaultInstance(emailProperties, null);
        emailMessage = new MimeMessage(mailSession);

        for (int i = 0; i < toEmails.length; i++) {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }

        emailMessage.setSubject(emailSubject);
        emailMessage.setContent(emailBody, "text/html"); //for a html email
        //emailMessage.setText(emailBody);// for a text email

    }


    public void sendEmailifJobCompleted() throws AddressException, MessagingException {

        String emailHost = "smtp.gmail.com";
        String fromUser = "******"; //just the id alone without @gmail.com
        String fromUserEmailPassword = "555555"; //

        Transport transport = mailSession.getTransport("smtp");

        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");
    }

    public void sendEmailifJobnotCompleted() throws AddressException, MessagingException {

        String emailHost = "smtp.gmail.com";
        String fromUser = "*******"; //just the id alone without @gmail.com
        String fromUserEmailPassword = "*******";

        Transport transport = mailSession.getTransport("smtp");

        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");

    }

}

当我尝试发送电子邮件时会发生错误

Error while reading file line by line:534-5.7.14 <https://accounts.google.com    /ContinueSignIn?sarp=1&scc=1&plt=AKgnsbvkh
534-5.7.14 6t9cTpTGLup7iypGocpm3F6Gpctby3p0fxBXXKwv5Raa66nOfkB0aGNeHVkyJcT2aeSmRc
534-5.7.14 3BLMYylXdaxOEyeOWQl2qg3DJi7_kz16sDVINej3vNJS1e8-tEheczcXZtM8nsSY4_FlE0
534-5.7.14 sCKCE-XoKvz-c-wddfDZRh_a7RvVn0VZH1LIsw05rJlsHhHbeQtaHeXHjZSjBJdT-nfjNn
534-5.7.14 Kfyl0HA> Please log in via your web browser and then try again.
534-5.7.14 Learn more at https://support.google.com/mail/bin/answer.py?answer=787
534 5.7.14 54 cz3sm1737237pbc.9 - gsmtp

现在当发生这种情况时gmail(在Gmail帐户中)询问是否可以安全地接收来自该帐户的电子邮件,我已指定“是”,现在它会收到电子邮件。我想知道如何进一步克服这个问题,例如有人更改了电子邮件地址吗?感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

如果您在发送前使用oAuth进行身份验证,则不会通过浏览器询问您。

https://developers.google.com/gmail/oauth_overview