java gmail 发送邮件,Java通过Gmail发送电子邮件

The following code is supposed to send email via gmail but it gives the following error:

DnGoq.jpg

On my gmail account I get a message that a sign in was prevented and I should use a secure app like gmail to access my account. The source code is as shown below:

public void doSendMail(){

username = txtFrom.getText();

password= new String(txtPassword.getPassword());

to = txtTo.getText();

subject = txtSubject.getText();

email_body = jTextArea1.getText();

Properties props = new Properties();

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

props.put("mail.smtp.socketFactory.port", "587");

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.port", "587");

Session session = Session.getDefaultInstance(props,

new javax.mail.Authenticator(){

@Override

protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(username, password);

}

}

);

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(username));

message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to));

message.setSubject(subject);

message.setText(email_body);

Transport.send(message);

JOptionPane.showMessageDialog(this, "Message Sent!","Sent",JOptionPane.INFORMATION_MESSAGE);

} catch (Exception e) {

JOptionPane.showMessageDialog(this, e.toString());

}

}

What can I do to the code to make it send mail via gmail?

解决方案

Your source code is perfect for sending email via gmail. May be you have to allow your account for less secure access via https://www.google.com/settings/security/lesssecureapps

Here is your code. I made very little modification to run as standalone program. It requires two jars : 1) mail-1.4.7.jar and 2) activation-1.1.1.jar

import java.util.Properties;

import java.util.Scanner;

import javax.mail.Message;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import javax.swing.JOptionPane;

/**

* Following jar are required:

* 1) mail-1.4.7.jar from http://central.maven.org/maven2/javax/mail/mail/1.4.7/mail-1.4.7.jar

* 2) activation-1.1.1.jar from http://central.maven.org/maven2/javax/activation/activation/1.1.1/activation-1.1.1.jar

*

*/

public class Test {

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.print("gmail username: ");

String username = sc.next();

System.out.print("gmail password: ");

String password = sc.next();

System.out.print("destination email address: ");

String to = sc.next();

System.out.print("subject: ");

String subject = sc.next();

System.out.print("email body: ");

String email_body = sc.next();

Test test = new Test();

test.doSendMail(username, password, to, subject, email_body);

sc.close();

}

// sends mail

public void doSendMail(final String username, final String password, String to, String subject, String email_body) {

Properties props = new Properties();

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

props.put("mail.smtp.socketFactory.port", "587");

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", "true");

props.put("mail.smtp.port", "587");

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {

@Override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(username, password);

}

});

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress(username));

message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

message.setSubject(subject);

message.setText(email_body);

Transport.send(message);

System.out.println("message sent");

JOptionPane.showMessageDialog(null, "Message Sent!", "Sent", JOptionPane.INFORMATION_MESSAGE);

} catch (Exception e) {

System.out.println(e);

JOptionPane.showMessageDialog(null, e.toString());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值