⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendmail.java

📁 本书中的源代码是以JBuilder工程形式组织的
💻 JAVA
字号:
package javamail;

import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;

public class SendMail {
  public SendMail() {
  }

  public void send() {
    try {
      //使用JNDI查询Mail Session,
     Hashtable ht = new Hashtable();
     ht.put(Context.INITIAL_CONTEXT_FACTORY,
         "weblogic.jndi.WLInitialContextFactory");
     ht.put(Context.PROVIDER_URL, "t3://localhost:7001");
     InitialContext ic = new InitialContext(ht);
      Session session = (Session) ic.lookup("MyMailSession ");

      //设置属性,然后创建Session实例
      Properties props = new Properties();
      props.put("mail.transport.protocol", "smtp");
      props.put("mail.smtp.host", "realmailserver.com");
      InternetAddress emailAddress = new InternetAddress(
          "test@realmailserver.com");
      props.put("mail.from", emailAddress);
      Session session2 = session.getInstance(props);

      //创建消息
      Message msg = new MimeMessage(session2);
      msg.setFrom();
      String to = "test1@realmailserver.com";
      msg.setRecipients(Message.RecipientType.TO,
                        InternetAddress.parse(to, false));
      String subject = "hello";
      msg.setSubject(subject);
      msg.setSentDate(new Date());
    // 将消息内容存放到MimeBodyPart对象中,然后把MimeBodyPart对象
    //存入Multipart对象中,最后,把代表附件的Multipart对象加入待
    //发送的消息中
      MimeBodyPart mbp = new MimeBodyPart();
      String messageTxt = "first mail ";
      mbp.setText(messageTxt);
      Multipart mp = new MimeMultipart();
      mp.addBodyPart(mbp);
      msg.setContent(mp);
      //发送消息
      Transport.send(msg);
    }
    catch (NamingException e) {

    }
    catch (MessagingException e) {

    }

  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -