1

我的申请需要向选定的人发送电子邮件。我的应用程序完全基于 Java。我什至不知道如何将我的 gmail 帐户用作 SMTP 邮件。

4

3 回答 3

1

这是一个示例,更改frompassto根据您的要求

String host = "smtp.gmail.com";
String from = "username";
String pass = "password";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true"); // added this line
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");

String[] to = {"to@gmail.com"}; // added this line

Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));

InternetAddress[] toAddress = new InternetAddress[to.length];

// To get the array of addresses
for( int i=0; i < to.length; i++ ) { // changed from a while loop
    toAddress[i] = new InternetAddress(to[i]);
}
System.out.println(Message.RecipientType.TO);

for( int i=0; i < toAddress.length; i++) { // changed from a while loop
    message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject("sending in a group");
message.setText("Welcome to JavaMail");
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
于 2012-12-26T11:57:06.507 回答
0

是您需要的所有信息。

于 2012-12-26T11:57:35.110 回答
0

如果您在 Windows 中,则可以将 Fake Sendmail 与 wamp 服务器一起使用,它的效果很好,她是如何安装和配置它的教程:

http://glob.com.au/sendmail/

于 2012-12-26T11:58:27.683 回答