当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:14:00  来源:本站整理

操纵JavaMail发送邮件[Java编程]

赞助商链接



  本文“操纵JavaMail发送邮件[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

代码以下:

package demo;

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import sun.misc.BASE64Encoder;

public class MailSender ...{

  public static void main(String[] args) ...{

    MailSender sender = new MailSender();
    sender.setHost("smtp.sina.com.cn");
    sender.setFromAddr("");
    sender.setToAddr("");
    sender.setUsername("wwwwwwww");
    sender.setPassword("ssssssss");
    sender.setTitle("关于提高效率的几点设法");
    sender.setAttachPath("C:singleUser.sql");
    sender.setAttachName("singleUser.sql");

    try ...{
      sender.sendMail();
    } catch (Exception e) ...{
      e.printStackTrace();
    }
  }

  public void sendMail() throws Exception...{

    Properties props = new Properties();
    props.put("mail.smtp.host", getHost());
    props.put("mail.smtp.auth", "true");
    Session session = Session.getDefaultInstance(props);
    session.setDebug(true);
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(getFromAddr()));
    message.addRecipient(Message.RecipientType.TO,new InternetAddress(getToAddr()));
    message.setSubject(getTitle());
    Multipart multipart = new MimeMultipart();
    BodyPart contentPart = new MimeBodyPart();
    contentPart.setText("邮件的具体内容在此");
    multipart. addBodyPart(contentPart);
    if(getAttachPath() != null && getAttachName() != null)...{
      BodyPart attachmentPart= new MimeBodyPart();
      DataSource source = new FileDataSource(getAttachPath ());
      attachmentPart.setDataHandler(new DataHandler(source));
      BASE64Encoder enc = new BASE64Encoder();
      attachmentPart.setFileName("=?GBK?B? "+enc.encode(getAttachName().getBytes())+"?=");
      multipart.addBodyPart(attachmentPart);
    }
    message.setContent(multipart);
    message.saveChanges();
    Transport transport = session.getTransport ("smtp");
    transport.connect(host, getUsername(), getPassword());
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();
  }

  private String host = null;
  private String fromAddr = null;
  private String toAddr = null;
  private String username = null;
  private String password = null;
  private String title = null;
  private String attachPath = null;
  private String attachName = null;

  public String getHost() ...{
    return host;
  }

  public void setHost(String host) ...{
    this.host = host;
  }

  public String getFromAddr() ...{
    return fromAddr;
  }

  public void setFromAddr(String fromAddr) ...{
    this.fromAddr = fromAddr;
  }

  public String getToAddr() ...{
    return toAddr;
  }

  public void setToAddr(String toAddr) ...{
    this.toAddr = toAddr;
  }

  public String getUsername() ...{
    return username;
  }

  public void setUsername(String username) ...{
    this.username = username;
  }

  public String getPassword() ...{
    return password;
  }

  public void setPassword(String password) ...{
    this.password = password;
  }

  public String getTitle() ...{
    return title;
  }

  public void setTitle(String title) ...{
    this.title = title;
  }

  public String getAttachPath() ...{
    return attachPath;
  }

  public void setAttachPath(String attachPath) ...{
    this.attachPath = attachPath;
  }

  public String getAttachName() ...{
    return attachName;
  }

  public void setAttachName(String attachName) ...{
    this.attachName = attachName;
  }
}


  以上是“操纵JavaMail发送邮件[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 操纵javascript得到浏览器中的星号密码
  • 操纵Java运算符
  • 传送和操纵Java对象
  • 操纵Java技术将Atom转换为RDF
  • 操纵java成立躲藏文件
  • <b>操纵Java实现网络传输数据压缩的实例</b>
  • 操纵Java来实现域名和IP地址转换的操作
  • <b>操纵Java实现一个简单的递归算法的实例</b>
  • <b>操纵Java编写网络通信程序</b>
  • 操纵Java Applet播放声音文件
  • 操纵Java语言举行Unicode代理编程
  • 操纵Java3D实现三维图象
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .