• JavaMail SMTP服务器发送邮件程序示例 java通过dns服务器解析ip地址


    /**
     * JavaMail SMTP服务器发送邮件程序示例
     * 扮演SMTP服务器角色与邮件客户端软件最大的区别就是:
     * SMTP服务器需要解析不同接收人邮件地址主机名对应的SMTP服务器主机名
     * ,同时不需要验证
     * 注意:由于条件的限制,此程序是运行不成功的,原因见下面的注释说明
     */
    
    import java.util.Date;
    import java.util.Properties;
    import javax.mail.Message.RecipientType;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;
    /**
     * @author Bill Tu
     * @since May 27, 2011(12:21:41 PM)
     *
     */
    public class SMTPSenderTest {
    	 public static String  getSMTPHost(String domain,String dnsServer) throws NamingException{
    		  DirContext ctx=new InitialDirContext();
    		  Attributes mxAttrs=null;
    		  if(null != dnsServer){
    			  mxAttrs=ctx.getAttributes("dns://"+dnsServer+"/"+domain,new String[]{"MX"});
    		  }else{
    			  mxAttrs=ctx.getAttributes("dns:/"+domain,new String[]{"MX"});
    		  }
    		  
    		  String mx=(String) mxAttrs.get("MX").get();
    		  String smtpServerName=mx.substring(mx.indexOf(" ")+1);
    		  return smtpServerName;
    	 }
    	 
    	 private static Message getMessage(Session session) throws AddressException, MessagingException{
    		  String from="iwtxokhtd@163.com";
    		  String subject="hello";
    		  String content="this is content";
    		  
    		  MimeMessage msg=new MimeMessage(session);
    		  msg.setFrom(new InternetAddress(from));
    		  msg.setSubject(subject);
    		  msg.setText(content);
    		  msg.setSentDate(new Date());
    		  
    		  return msg;
    	  
    	 }
    	 
    	 private static Session getSession(){
    		  Properties props=new Properties();
    		  //设置ehlo命令中的主机名,若检测到主机名与其IP地址一致,则认为这时的发送方是SMTP服务器,就不需要验证;否则就是邮件客户端软件,则需要验证。
    		  props.put("mail.smtp.localhost", "mail.digu.com");
    		  props.put("mail.smtp.auth", "false"); 
    		  props.put("mail.smtp.port", "25"); 
    		  Session session=Session.getDefaultInstance(props);
    		  session.setDebug(true);
    		  return session;
    	 }
    	 
    	 @SuppressWarnings("static-access")
    	 private static void sendEmail(Message message,Session session,String to) 
    	   throws AddressException, MessagingException, NamingException{
    		  message.setRecipients(RecipientType.TO, InternetAddress.parse(to));
    		  message.saveChanges();
    		  
    		  Transport transport=session.getTransport("smtp");
    		  
    		  String domain=to.substring(to.indexOf("@")+1);
    		  String host=getSMTPHost(domain,null);
    		  
    		  transport.connect(host, null, null);
    		  transport.send(message, message.getRecipients(Message.RecipientType.TO));
    		  
    		  transport.close();
    	 }
    	
    	 public static void main(String []args) 
    	  throws AddressException, MessagingException, NamingException{
    		  String []to=new String[]{"277515444@qq.com","277515445@qq.com"};
    		  Session session=getSession();
    		  Message msg=getMessage(session);
    		  for(String email:to){
    			  sendEmail(msg,session,email);
    		  }
    	 }
    }
    

      

  • 相关阅读:
    使用FluentScheduler和IIS预加载在asp.net中实现定时任务管理
    [WCF REST] Web消息主体风格(Message Body Style)
    [WCF REST] Web消息主体风格(Message Body Style)
    REST WCF Service中的WebMessageBodyStyle
    REST WCF Service中的WebMessageBodyStyle
    C#表示空字符
    053517
    053516
    053515
    053514
  • 原文地址:https://www.cnblogs.com/jifeng/p/6323687.html
Copyright © 2020-2023  润新知