【发布时间】:2019-03-08 01:42:26
【问题描述】:
这是我的 java 邮件 API 代码:
import java.io.File;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.activation.*;
import hostel.BabyAuthenticator;
class BabyAuthenticator extends Authenticator
{
@Override
protected PasswordAuthentication
getPasswordAuthentication()
{
PasswordAuthentication pa = new
PasswordAuthentication("my email-id","password of my account");
return pa;
}
}
我已经尝试了 2-3 个不同的电子邮件 ID 作为发件人的 ID ..但仍然出现错误。
public class SendMail {
public static void main(String[] args) {
try
{
Properties p = new Properties();
p.put("mail.smtp.host","smtp.gmail.com");
p.put("mail.smtp.port","587");
p.put("mail.smtp.starttls.enable","true");
p.put("mail.smtp.auth","true");
p.put("mail.smtp.ssl.trust", "smtp.gmail.com");
BabyAuthenticator auth = new BabyAuthenticator();
Session session = Session.getInstance(p,auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("sender's email id"));
InternetAddress receiver1 = new InternetAddress("receiver's email id");
InternetAddress[] rcvrs = {receiver1};
// specify the the type of Recipients
message.addRecipients(RecipientType.TO,rcvrs);
// provide the subject of email
message.setSubject("First mail");
// create object of MimeBodyPart to denote the body parts of mail
MimeBodyPart part1 = new MimeBodyPart();
// associate some text to the body part
part1.setContent("<i style='color : blue'>"+ "This is my first mail"+"
</i>","text/html");
MimeMultipart allParts = new MimeMultipart();
allParts.addBodyPart(part1);
message.setContent(allParts);
Transport.send(message);
// show message on console
System.out.println("Mail has been sent to the mail server...");
}
catch (Exception e)
{
System.out.println("Some error has occured, and error is "+e);
}
}
}
这是代码的输出:
发生了一些错误,错误是 javax.mail.AuthenticationFailedException:535-5.7.8 用户名和 不接受密码。在 535 5.7.8 了解更多信息 https://support.google.com/mail/?p=BadCredentialsi72sm13717325pfj.147 -gsmtp
【问题讨论】:
标签: java