php editor Apple has brought an article about automatic processing of Java Mail emails. This article will help you realize automated management of emails. Java Mail is a powerful tool that can help you automate email processing and improve work efficiency. Let's explore how to use Java Mail to automate email management!
Java Mail email automation can be used in a variety of scenarios, including:
The following is an example of sending an email using Java Mail:
import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PassWordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class JavaMailSendEmail { public static void main(String[] args) { // 收件人电子邮件地址 String to = "example@gmail.com"; // 发件人电子邮件地址 String from = "johndoe@gmail.com"; // 发件人电子邮件密码 String password = "secret"; // 发送电子邮件的SMTP服务器 String smtpHost = "smtp.gmail.com"; // SMTP端口 int smtpPort = 587; // 设置邮件服务器属性 Properties props = new Properties(); props.put("mail.smtp.host", smtpHost); props.put("mail.smtp.port", smtpPort); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); // 创建认证对象 PasswordAuthentication auth = new PasswordAuthentication(from, password); // 创建邮件会话 Session session = Session.getInstance(props, auth); try { // 创建邮件消息 Message message = new MimeMessage(session); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Java Mail示例"); message.setText("Hello, World!"); // 发送电子邮件 Transport.send(message); System.out.println("电子邮件已发送成功。"); } catch (MessagingException e) { System.out.println("电子邮件发送失败。"); e.printStackTrace(); } } }
Java Mail email automation can also be used for more advanced purposes, such as:
By using Java Mail email automatic processing, developers can easily realize automated email management and improve work efficiency.
The above is the detailed content of Java Mail email automation processing: realize automated email management. For more information, please follow other related articles on the PHP Chinese website!