我在eclipse中編寫這個應用程序我添加了所有的jar文件。我正在粘貼代碼和錯誤。請讓我知道我應該如何更改以正確運行應用程序。這段代碼有什麼問題。運行時錯誤
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendMail
{
public static void main(String [] args)
{
SendMail sm=new SendMail();
try{
sm.postMail(new String[]{"[email protected]"},"hi","hello","[email protected]");
}
catch(MessagingException e)
{
e.printStackTrace();
}
}
public void postMail(String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.port", "25");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
}
錯誤:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. 13sm646598ewy.13
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1829)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1368)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:886)
at javax.mail.Transport.send0(Transport.java:191)
at javax.mail.Transport.send(Transport.java:120)
at SendMail.postMail(SendMail.java:54)
at SendMail.main(SendMail.java:10)
要發送電子郵件到Gmail,從Java:http://stackoverflow.com/questions/46663/how-do-you-send-email-from -a-java-app-using-gmail – leonbloy 2010-04-29 17:58:44