所以我试图让
Java Mail工作,因为我在这个网站上看到的其他答案都没有用,我不得不假设在过去一年左右的时间里有些变化.所以,如果这个问题看起来像是一个重复,我无法解释为什么它不起作用.以下是我的代码:
try{ Properties property = new Properties(); property.setProperty("mail.smtp.host","smtp.gmail.com"); property.setProperty("mail.smtp.starttls.enable","true"); //property.setProperty("mail.smpt.port","25"); property.setProperty("mail.smtp.user","myEmail@gmail.com"); property.setProperty("mail.smtp.auth","true"); System.out.println("Mail Check 1"); Session session = Session.getDefaultInstance(property); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("myEmail@gmail.com")); message.addRecipient(Message.RecipientType.TO,new InternetAddress("myEmail@gmail.com")); System.out.println("Mail Check 2"); message.setSubject("Oil Error Report"); message.setText(emailMessage); System.out.println("Mail Check 3"); Transport transport = session.getTransport("smtps"); transport.connect("smtp.gmail.com",465,"myEmail@gmail.com","myPassword"); transport.sendMessage(message,message.getAllRecipients()); transport.close(); System.out.println("Mail Sent"); }catch(Exception ex){ System.out.println("Mail fail"); System.out.println(ex); }
我起床邮件检查3然后我得到以下异常:
com.sun.mail.smtp.SMTPSendFailedException: 530-5.5.1 Authentication required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 u3sm10254140ioi.27 - gsmtp
这没有意义,因为我将mail.smtp.auth设置为true.我已经查看了过去的答案,并且在发布之前我试图让它工作.我假设它很简单..任何帮助都会非常感激!谢谢!
解决方法
public class EmailSender { public void sendEmail(String emailMessage){ try{ final String fromEmail = ""; //requires valid gmail id final String password = ""; // correct password for gmail id final String toEmail = ""; // can be any email id System.out.println("TLSEmail Start"); Properties props = new Properties(); props.put("mail.smtp.host","smtp.gmail.com"); //SMTP Host props.put("mail.smtp.port","587"); //TLS Port props.put("mail.smtp.auth","true"); //enable authentication props.put("mail.smtp.starttls.enable","true"); //enable STARTTLS //create Authenticator object to pass in Session.getInstance argument Authenticator auth = new Authenticator() { //override the getpasswordAuthentication method protected PasswordAuthentication getpasswordAuthentication() { return new PasswordAuthentication(fromEmail,password); } }; Session session = Session.getInstance(props,auth); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(fromEmail)); message.addRecipient(Message.RecipientType.TO,new InternetAddress(toEmail)); System.out.println("Mail Check 2"); message.setSubject("Oil Error Report"); message.setText(emailMessage); System.out.println("Mail Check 3"); Transport.send(message); System.out.println("Mail Sent"); }catch(Exception ex){ System.out.println("Mail fail"); System.out.println(ex); } } }
这是使它运行所需的代码!它是2014年11月,目前适用于Gmail!我希望这段代码可以帮助节省时间,占用我的大部分时间!除此代码外,您还必须更改电子邮件设置,以允许允许使用这些电子邮件.您第一次尝试时会收到来自Google的电子邮件,它会指导您更改设置.
谢谢,祝你好运!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。