日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java邮件发送

發(fā)布時(shí)間:2024/1/23 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java邮件发送 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 需要的jar包

?activation.jar,mail.jar,mailapi.jar


2?SendMailObject.java

package com.mc.common.javasendmail;/*** @author Zhangkun aistill@msn.com* @version 1.0*/import java.util.*; import javax.mail.*; import javax.mail.internet.*;import java.util.Date; import javax.activation.*; import java.io.*;public class SendMailObject {// ���巢���?��?��?������private String to = "";private String from = "";private String cc = "";private String bcc = "";private String bounceback = "";private String subject = "";private String TextContent = "";private String SMTPServer = "";private String filename = "";private String sasluser = "";private String saslpass = "";private boolean isHtmlMail = true;// ���?��淢�?������?���?���Vector file = new Vector();public SendMailObject(String SMTPServer) {this.SMTPServer = SMTPServer;}public SendMailObject(String SMTPServer, String SASLUser, String SASLPass) {// the mail server need to saslauth,initialize tthe sasl parameterthis.SMTPServer = SMTPServer;this.sasluser = SASLUser;this.saslpass = SASLPass;}public void setMailInfo(String from, String to, String cc, String bcc,String bounceback, String subject, boolean isHtmlMail,String TextContent) {// ��?�������?��?��?������this.from = from;this.to = to;this.cc = cc;this.bcc = bcc;this.bounceback = bounceback;this.subject = subject;this.isHtmlMail = isHtmlMail;this.TextContent = TextContent;}// �÷��������?�������public void attachfile(String fname) {file.addElement(fname);}// ��?�����?��?���public boolean startSendMail() {try {// ����properties����Properties props = System.getProperties();// �����?�������props.put("mail.smtp.host", SMTPServer);// �����?������� ����?��?��? ���?������?�?props.put("mail.smtp.from", bounceback);// set the smtp auth option of the mail serverif ("".equalsIgnoreCase(sasluser.trim())) {props.put("mail.smtp.auth", "false");System.out.println("set smtp sasl authentication mail.smtp.auth = false");} else {props.put("mail.smtp.auth", "true");System.out.println("set smtp sasl authentication mail.smtp.auth = true");}// �?�?�???�����Session session = Session.getDefaultInstance(props, null);// ����?����?������?������?�?���?��MimeMessage msg = new MimeMessage(session);// �����?�?�?�������msg.setSentDate(new Date());// ���?�?msg.setFrom(new InternetAddress(from));// ���?�?msg.setRecipients(Message.RecipientType.TO, to);// ���?�?if (!"".equalsIgnoreCase(cc.trim())) { // if the cc is not null ,set// the cc field of the mailmsg.setRecipients(Message.RecipientType.CC, cc);}// �����?�?if (!"".equalsIgnoreCase(bcc.trim())) { // if the cc is not null// ,set the cc field of the// mailmsg.setRecipients(Message.RecipientType.BCC, bcc);}// ����msg.setSubject(subject);// �����bodypart�����?�?�������multipart��Multipart mp = new MimeMultipart();// add attachment , ����?��������?����Enumeration efile = file.elements();// ����������?��и��?���while (efile.hasMoreElements()) {MimeBodyPart mbp = new MimeBodyPart();// ?���??��������filename = efile.nextElement().toString();// �?����?FileDataSource fds = new FileDataSource(filename);// �?��������?����bodypartmbp.setDataHandler(new DataHandler(fds));// �?��?���?������bodypartmbp.setFileName(fds.getName());mp.addBodyPart(mbp);}// ���?����е�����?��file.removeAllElements();// add mail text bodyMimeBodyPart mbpc = new MimeBodyPart();if (isHtmlMail == true) {mbpc.setContent("<meta http-equiv='Content-Type' content='text/html; charset=gbk' />"+ TextContent, "text/html;charset=GB2312");} else {mbpc.setText(TextContent);}mp.addBodyPart(mbpc);// multipart���?�?�msg.setContent(mp);msg.saveChanges();System.out.println(new Date() + " sending a mail by Mail Server:"+ SMTPServer + " .....");// �����?�Transport transport = session.getTransport("smtp");transport.connect((String) props.get("mail.smtp.host"), sasluser,saslpass);if ("".equalsIgnoreCase(sasluser.trim())) { //transport.send(msg);} else {transport.sendMessage(msg, InternetAddress.parse(to));if (!"".equalsIgnoreCase(cc.trim())) { // if the cc is not null// ,sent mailtransport.sendMessage(msg, InternetAddress.parse(cc));}if (!"".equalsIgnoreCase(bcc.trim())) { // if the cc is not null// ,sent mailtransport.sendMessage(msg, InternetAddress.parse(bcc));}}System.out.println("Send mail completed!");} catch (MessagingException mex) {mex.printStackTrace();Exception ex = null;if ((ex = mex.getNextException()) != null) {ex.printStackTrace();}return false;}return true;}}

3 EmailTest.java?


package com.cpcnet.test;import com.mc.common.javasendmail.SendMailObject;public class EmailTest {public static void main(String[] args) {String email_from = "****@qq.com";String email_to = "***@**-***.net";String smtp_host = "1s2m9t4p2.9h0k2.0n1et";String email_cc = "";String email_reject_to = "";String email_subject = "test";String sbEmailBody = "test";// SendMailObject mail=new SendMailObject(sync.smtp_host); SendMailObject mail=new SendMailObject(smtp_host); // mail.setMailInfo(sync.email_from, sync.email_to, sync.email_cc,"",sync.email_reject_to,sync.email_subject,true,sbEmailBody.toString());mail.setMailInfo(email_from, email_to, email_cc,"",email_reject_to,email_subject,true,sbEmailBody.toString());mail.startSendMail();mail = null;}}

總結(jié)

以上是生活随笔為你收集整理的java邮件发送的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。