当前位置:主页 > 查看内容

邮件发送简单例子-bean文件

发布时间:2021-09-14 00:00| 位朋友查看

简介:SimpleSendMessage.java import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*; public class SimpleSendMessage { public static void main(String[] args) { // Collect the necessary information to send……
SimpleSendMessage.java

import java.util.*;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SimpleSendMessage {

public static void main(String[] args) {

// Collect the necessary information to send a simple message
// Make sure to replace the values for host, to, and from with
// valid information.
// host - must be a valid smtp server that you currently have
// access to.
// to - whoever is going to get your email
// from - whoever you want to be. Just remember that many smtp
// servers will validate the domain of the from address
// before allowing the mail to be sent.
String host = "server.myhost.com";
String to = "YourFriend@somewhere.com";
String from = "MeMeMe@myhost.com";
String subject = "JSP Rules!";
String messageText = "I am sending a message using the"
+ " JavaMail API.\nI can include any text that I want.";
boolean sessionDebug = false;

// Create some properties and get the default Session.
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol", "smtp");

Session session = Session.getDefaultInstance(props, null);

// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// will.
session.setDebug(sessionDebug);

try {

// Instantiate a new MimeMessage and fill it with the
// required information.
Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

// Hand the message to the default transport service
// for delivery.
Transport.send(msg);
}
catch (MessagingException mex) {

mex.printStackTrace();
}
}
}

本文转载自网络,原文链接:https://m.jb51.net/article/2498.htm
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:jsp源码实例5(cookie) 下一篇:没有了

推荐图文


随机推荐