如何在netbeans swing应用程序中使用javamail api发送电子邮件? [英] How do I send an email using javamail api in netbeans swing application?

查看:82
本文介绍了如何在netbeans swing应用程序中使用javamail api发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为客户查询开发了一个小应用程序?



它的用户可以在几个文本框中输入客户详细信息,例如,



客户名称

客户电话号码

客户问题。



用户后完成输入数据。有一个提交按钮,一旦用户点击它。

详细信息将发送给另一个人。

为例:经理人。



i使用过java邮件api?对于那个。



它将成功构建程序



但同时它将给出一个异常错误。



线程AWT-EventQueue-0中的异常java.lang.RuntimeException:javax.mail.AuthenticationFailedException:534-5.7.14< https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvG

534-5.7.14 5tY-sFXWUo7QgeGofeaJDvflnO7Pt-_gbScu4j3cTrL_lT6rDn3R7EXWz-CElFeN9XwzWc

534-5.7.14 qYMVg5da2ff4UGzc1EUBDnTyzHHCG-Y-SZ-zMkflYTg7rXRmmNesZ8C-OoSYaGt642vj2z

534-5.7.14 njfbhASCvcai7lV7dTvDdH79szsHKW8rD1XGD11SYDp-oc4BIrp88HoqamghSPfEp1T8CH

534-5.7.14 Mx49SUYyhUDtp​​w5J6HlFT6eoF9588>请通过您的网络浏览器登录和

534-5.7.14然后再试一次。

534-5.7.14了解更多信息

534 5.7.14 https://support.google.com/mail/answer/78754 o71sm37079708pfj.68 - gsmtp



我尝试过:



private void enqSubActionPerformed(java.awt.event.ActionEvent evt){



final String username =hemastravelsit@gmail.com;

final String password =Hemas @ 9000;



属性props = new属性() ;

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

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

props.put(mail.smtp.host,smtp.gmail.com); //这是smtp服务器地址

道具。 put(mail.smtp.port,587); //这是smtp服务器的端口



会话session = Session.getInst ance(props,

new javax.mail.Authenticator(){

protected PasswordAuthentication getPasswordAuthentication(){

返回新的PasswordAuthentication(用户名,密码);

}

});



尝试{

留言= new MimeMessage(session);

message.setFrom(new InternetAddress(hemastravelsit@gmail.com)); //来自emailaddress

message.setRecipients(消息。 RecipientType.TO,InternetAddress.parse(businesssupport.travels@hemas.com)); //发送电子邮件地址

message.setSubject(我的第一封电子邮件); //设置电子邮件主题

message.setContent(< h:body style =background-color:white; font-family:verdana:color:#0066CC:xmlns:h =#unknown>

+如果你这样写了你写的第一封电子邮件!



+&l吨; /体> 中, text / html的; charset = utf-8); //设置电子邮件的内容正文

Transport.send(message); //发送整个邮件对象

System.out .println(被发送的电子邮件:完成); //验证代码被解雇

}

catch(MessagingException e){

抛出新的RuntimeException(e); //如果电子邮件地址不好或者不感兴趣

}



}

i have developed a small application for customer inquiries?

its like user can enter customer details to several text boxes like,

Customer name
Customer telephone number
Customer issue.

after user finished entering data. there is a submit button and once user click it.
the details will send to another person.
as an example : manager person.

i have used java mail api? for that.

it will build the program successfully

but on the same time it will give an exception error as well.

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvG
534-5.7.14 5tY-sFXWUo7QgeGofeaJDvflnO7Pt-_gbScu4j3cTrL_lT6rDn3R7EXWz-CElFeN9XwzWc
534-5.7.14 qYMVg5da2ff4UGzc1EUBDnTyzHHCG-y-sZ-zMkflYTg7rXRmmNesZ8C-OoSYaGt642vj2z
534-5.7.14 njfbhASCvcai7lV7dTvDdH79szsHKW8rD1XGD11SYDp-oc4BIrp88HoqamghSPfEp1T8CH
534-5.7.14 Mx49SUYyhUDtpw5J6HlFT6eoF9588> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 o71sm37079708pfj.68 - gsmtp

What I have tried:

private void enqSubActionPerformed(java.awt.event.ActionEvent evt) {

final String username = "hemastravelsit@gmail.com";
final String password = "Hemas@9000";

Properties props = new Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host","smtp.gmail.com");// this is the smtp server address
props.put("mail.smtp.port","587");//This is the port for the smtp server

Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username, password);
}
});

try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("hemastravelsit@gmail.com"));//from emailaddress
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("businesssupport.travels@hemas.com"));//to email address
message.setSubject("My first Email");//sets subject of email
message.setContent("<h:body style="background-color:white;font-family:verdana:color:#0066CC:" xmlns:h="#unknown">"
+"if you are getting this you wrote your first email!

"
+"</body>","text/html; charset=utf-8");//Sets the content body of the email
Transport.send(message);//sends the entire message object
System.out.println("was the email sent: Done");// verifies that the code fired
}
catch(MessagingException e){
throw new RuntimeException(e);// If the email address is bad or doesnt excist
}

}

推荐答案

需要注意的一点是,在大多数Google SMTP服务器的情况下,如果您的帐户不允许,它将不允许直接使用SMTP。在这种情况下,我看到许多开发人员编写了协议API的正确实现,但他们仍然无法发送电子邮件。在这种情况下,请确保您的帐户允许通过身份验证进行SMTP通信。它在您的设置中,检查它。



后来,问题仍然是authenti阳离子到您的帐户。例如,请参阅错误消息:



One thing to note is that, in most of the cases of Google SMTP server, is that it won't allow SMTP consumption directly, if your account doesn't allow. In many cases, I have seen that many developers have written a correct implementation of the protocol API, but still they cannot send the email. In that case, make sure that your account allows SMTP communication through your authentication. It is in your settings, check it.

Later, the problem is still the authentication to your account. For example, see the error message:

Quote:

javax.mail。 AuthenticationFailedException ...请通过Web浏览器登录,然后重试。

javax.mail.AuthenticationFailedException...Please log in via your web browser and then try again.

这表示您需要对系统进行身份验证才能使用该帐户。 Google不允许您的应用使用此功能。



以下其中一项原因可能是:



1.您的帐户是新帐户。谷歌打击垃圾邮件;或者这就是他们告诉我们的。 :笑:

2.确保您可以从网络浏览器登录帐户,试试运气: mail.google。 com

3.确保按照安全标准支持应用程序; SSL,端口和身份验证等。

4.更多。



这些是您需要保留的一些主要内容心神。您的代码是正确的,问题在于身份验证。用户名/密码不正确,否则Google会阻止您的应用进行通信。



此外,帮助网址为:可以登录我的电子邮件应用 - Gmail帮助 [ ^ ],你使用的是错误的。 :-)



如需更多信息,请阅读以下内容:



JavaMail API&#8211;通过Gmail SMTP示例发送电子邮件 [ ^ ]

java - 解决错误javax.mail.AuthenticationFailedException - 堆栈溢出 [ ^ ]

This tells that you need to authenticate the system to use that account. Google does not allow your application to use this.

One of the following may be the reason:

1. Your account is a new one. Google fights against spam; or that is what they tell us. :laugh:
2. Make sure you can sign in to account from web browser, try your luck at: mail.google.com.
3. Make sure application is supported as per security standards; SSL, port and authentication etc.
4. Much more.

These are a few of the major things that you need to keep in mind. Your code is correct, the problem is with authentication. Either the username/password is incorrect, otherwise Google is preventing your app to communicate.

Also, the help URL is: Can’t sign in to my email app - Gmail Help[^], you are using the wrong one. :-)

For more, please read these:

JavaMail API &#8211; Sending email via Gmail SMTP example[^]
java - Solve error javax.mail.AuthenticationFailedException - Stack Overflow[^]


这篇关于如何在netbeans swing应用程序中使用javamail api发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆