1.メールボックスサーバーの概要
1.1。メールボックスサーバーの基本概念
- メールクライアント:コンピューターまたはWebページの形式でのみインストールできます
- メールサーバー:メールの受信とプッシュの役割を果たします
- メール送信用プロトコル:POP3、IMAP4、SMTP
2.2。メール送信プロセス
簡単に言えば、私たちはメールを送信するか、他の人から送信されたメールを受け入れます。IMAP4プロトコルとSMTPプロトコルはそれを実現するのに役立ちます。
メールを取得し、メールを読み、右クリックすることはすべてPOP3プロトコルです。
3.3。メールボックスサーバーのインストール
ステップ1:メールボックスサーバーソフトウェアをダブルクリックします
-
リンク:https
://pan.baidu.com/s/1rqpiaJOTI5j9ZSNB01ROyw抽出コード:199j
手順2:メールボックスサーバーを構成する
インターフェイスをダブルクリックして、[ツール]-> [サーバー設定]-> [サーバーのコンテンツを設定]を見つけます。
ステップ3:新しいアカウントを作成する
インターフェイスに戻る->アカウント->新しいアカウント
4.クライアントのインストールを電子メールで送信
App StoreでFormailをダウンロードして
正常に登録した後、[設定]-> [システム]-> [アカウント設定]を見つけ、サーバーに入力された対応するアカウントを作成します。
2つ目は、コードを使用してメールを送信する
ステップ1:jarパッケージをインポートします
ステップ2:MainUtilsを変更する
public class MailUtils {
//email:邮件发给谁 subject:表示主题 emailMsg:邮件的内容
public static void sendMail(String email,String subject, String emailMsg)
throws AddressException, MessagingException, GeneralSecurityException {
//创建一个程序与邮件服务器会话对象Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");//发邮件协议——SMTP
props.setProperty("mail.host", "smtp.qq.com"); //发送邮件的服务器地址
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
props.put("mail.smtp.ssl.socketFactory",sf);
//创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("2322517484", "032426zyl,.");//发邮件的账号的验证
}
};
//此时并非HttpSession
Session session = Session.getInstance(props, auth);
// 创建一个Message,相当于邮件的内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 谁是接收者
message.setSubject(subject);
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用户将邮件发送
Transport.send(message);
}
}
3番目のステップ:テストを実行します。
public class SendMailTest {
public static void main(String[] args) throws GeneralSecurityException {
try {
MailUtils.sendMail("[email protected]","测试","此为此时邮件");
} catch (AddressException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3、定期的にメールデモを送信する
ステップ1:データベースを準備する
ステップ2:パッケージをガイドする
ステップ4:テストを行う
public class BirthdayListener implements ServletContextListener{
@Override
public void contextInitialized(ServletContextEvent sce) {
//当web应用启动开启任务调动——功能在用户的生日当天发送邮件
Timer timer =new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
// 为当前的生日的用户进行发邮件
//1.获得当前过生日的人
//获得今天过生日人的信息
SimpleDateFormat format =new SimpleDateFormat("MM-dd");
String currentDate= format.format(new Date());
//根据当前时间从数据查询今天过生日的人
QueryRunner queryRunner =new QueryRunner(DataSourceUtils.getDataSource());
String sql="SELECT * FROM users WHERE birthday like ?";
List <User>users=null;
try {
users= queryRunner.query(sql,new BeanListHandler<User>(User.class),"%"+currentDate);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//发邮件
if (users !=null&&users.size()>0) {
for (User user : users) {
String emailMsg="亲爱的"+user.getUsername()+"生日快了";
try {
MailUtils.sendMail(user.getEmail(), "生日祝福", emailMsg);
System.out.println("邮件发送完毕");
} catch (MessagingException | GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
},new Date(),1000*60*60*24);//实际开发中起始时间一定是一个固定的时间
//实际开发中你的间隔时间为一天,每一天取出当天过生日的人,发送邮件
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
手順5:web.xmlファイルを構成する
<listener>
<listener-class>com.listener.birthday.BirthdayListener</listener-class>
</listener>
6番目のステップ
実装する必要がある場合は、MailUtilsで自分でいくつかのプロパティを設定できます
public class MailUtils {
//email:邮件发给谁 subject:表示主题 emailMsg:邮件的内容
public static void sendMail(String email,String subject, String emailMsg)
throws AddressException, MessagingException, GeneralSecurityException {
//创建一个程序与邮件服务器会话对象Session
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "SMTP");//发邮件协议——SMTP
props.setProperty("mail.host", "smtp.qq.com"); //发送邮件的服务器地址
props.setProperty("mail.smtp.auth", "true");// 指定验证为true
//创建验证器
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("2322517484", "032426zyl,.");//发邮件的账号的验证
}
};
//此时并非HttpSession
Session session = Session.getInstance(props, auth);
// 创建一个Message,相当于邮件的内容
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("[email protected]")); // 设置发送者
message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 谁是接收者
message.setSubject(subject);
message.setContent(emailMsg, "text/html;charset=utf-8");
// 3.创建 Transport用户将邮件发送
Transport.send(message);
}
}