我的程序代码如下:importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjava.util.Properties;importjavax.mail.Session;importjava.io.FileOu...
我的程序代码如下:
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import javax.mail.Session;
import java.io.FileOutputStream;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.Message;
import javax.mail.Transport;
public class HtmlMessageSender
{
String protocol = "smtp";
String from = "lebrondomiante@sina.cn";
String to = "这里写的是我的一个gmail账户";
String subject = "test";
String body = "Hello!!"+
"";
public static void main(String[] args) throws Exception
{
String server = "smtp.sina.com.cn";
String user = "我的用户名";
String pass = "我的密码";
HtmlMessageSender sender = new HtmlMessageSender ();
Session session = sender.createSession();
MimeMessage message = sender.createMessage(session);
Transport transport = session.getTransport();
transport.connect(server, user, pass);
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}
public Session createSession()
{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", protocol);
props.setProperty("mail.smtp.auth","true");
Session session = Session.getInstance(props);
session.setDebug(true);
return session;
}
public MimeMessage createMessage(Session session) throws Exception
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
MimeMultipart multipart = new MimeMultipart("related");
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body, "text/html;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
MimeBodyPart gifBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("E:\\lebron james\\images.jpg");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("LEBRON");
multipart.addBodyPart(gifBodyPart);
message.setContent(multipart);
message.saveChanges();
return message;
}
}
然后运行后出错:
535 #5.7.0 Authentication failed
Exception in thread "main" javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at HtmlMessageSender.main(HtmlMessageSender.java:33)
请教高手是哪里出了问题,我现在人在澳大利亚,这个和我不在国内有关系吗?
展开