Java通过Gmail发送电子邮件

编程入门 行业动态 更新时间:2024-10-27 10:33:27
本文介绍了Java通过Gmail发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下代码应该通过gmail发送电子邮件,但它会给出以下错误:

在我的gmail帐户中,我收到一条消息,说明登录被阻止,我应该使用安全像Gmail的应用程序来访问我的帐户。源代码如下所示:

public void doSendMail(){ username = txtFrom.getText(); password = new String(txtPassword.getPassword()); to = txtTo.getText(); subject = txtSubject.getText(); email_body = jTextArea1.getText(); 属性props = new Properties(); props.put(mail.smtp.host,smtp.gmail); props.put(mail.smtp.socketFactory.port,587); props.put(mail.smtp.socketFactory.class,javax.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(){返回新的PasswordAuthentication(用户名,密码); } } ); 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()); $ / code>

我能做些什么来让代码发送mail via gmail?

解决方案

您的源代码非常适合通过Gmail发送电子邮件。可能是您必须通过 www.google/settings/security/lesssecureapps

$允许您的帐户访问安全性较低b $ b

这是您的代码。我做了非常小的修改以作为独立程序运行。它需要两个罐子:1)mail-1.4.7.jar和2)activation-1.1.1.jar

import java .util.Properties; import java.util.Scanner; import javax.mail.Message; import javax.mail.PasswordAuthentication; 导入javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.swing.JOptionPane; / ** *需要以下jar: * 1)mail-1.4.7.jar from central.maven/maven2/javax/ mail / mail / 1.4.7 / mail-1.4.7.jar * 2)来自central.maven/maven2/javax/activation/activation/1.1的activation-1.1.1.jar。 1 / activation-1.1.1.jar * * / public class Test { public static void main(String args []){扫描仪sc =新扫描仪(System.in); System.out.print(gmail username:); 字符串用户名= sc.next(); System.out.print(gmail password:); String password = sc.next(); System.out.print(目标电子邮件地址:); 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(用户名,密码,主题,email_body); sc.close(); $ b $ //发送邮件 public void doSendMail(最终字符串用户名,最终字符串密码,字符串到,字符串主题,字符串email_body){ 属性props = new Properties(); props.put(mail.smtp.host,smtp.gmail); props.put(mail.smtp.socketFactory.port,587); props.put(mail.smtp.socketFactory.class,javax.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(用户名,密码); } }); 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()); } } }

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

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"); props.put("mail.smtp.socketFactory.port", "587"); props.put("mail.smtp.socketFactory.class", "javax.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 www.google/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 central.maven/maven2/javax/mail/mail/1.4.7/mail-1.4.7.jar * 2) activation-1.1.1.jar from central.maven/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"); props.put("mail.smtp.socketFactory.port", "587"); props.put("mail.smtp.socketFactory.class", "javax.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()); } } }

更多推荐

Java通过Gmail发送电子邮件

本文发布于:2023-10-07 10:26:04,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送电子邮件   Java   Gmail

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!