導航:首頁 > 編程語言 > java發送郵件表格

java發送郵件表格

發布時間:2022-08-08 00:15:50

java 發送郵件,內容是要從資料庫中讀取的數據並列成表格的狀態發送出去

publicbooleansendTextMail(MailSenderInfomailInfo){
//判斷是否需要身份認證
MyAuthenticatorauthenticator=null;
Propertiespro=mailInfo.getProperties();
if(mailInfo.isValidate()){
//如果需要身份認證,則創建一個密碼驗證器
authenticator=newMyAuthenticator(mailInfo.getUserName(),mailInfo.getPassword());
}
//根據郵件會話屬性和密碼驗證器構造一個發送郵件的session
SessionsendMailSession=null;


//sendMailSession=Session.getDefaultInstance(pro,authenticator);//獲取默認可能報錯
sendMailSession=Session.getInstance(pro,authenticator);//新創建一個session
if(sendMailSession==null){
System.out.println("無法獲取郵件郵件Session");
}
try{
//根據session創建一個郵件消息
MessagemailMessage=newMimeMessage(sendMailSession);
//創建郵件發送者地址
Addressfrom=newInternetAddress(mailInfo.getFromAddress());
//設置郵件消息的發送者
mailMessage.setFrom(from);
//創建郵件的接收者地址,並設置到郵件消息中
Addressto=newInternetAddress(mailInfo.getToAddress());

mailMessage.setRecipient(Message.RecipientType.TO,to);

//設置郵件消息的主題
mailMessage.setSubject(mailInfo.getSubject());
//設置郵件消息發送的時間
mailMessage.setSentDate(newDate());
//設置郵件消息的主要內容
StringmailContent=mailInfo.getContent();
mailMessage.setText(mailContent);//添加附件
//if(mailInfo.getAttachFileNames()!=null||mailInfo.getAttachFileNames().length>0){
//Multipartmp=newMimeMultipart();
//MimeBodyPartmbp=null;
//for(StringfileName:mailInfo.getAttachFileNames()){
//mbp=newMimeBodyPart();
//FileDataSourcefds=newFileDataSource(fileName);//得到數據源
//mbp.setDataHandler(newDataHandler(fds));//得到附件本身並至入BodyPart
//mbp.setFileName(fds.getName());//得到文件名同樣至入BodyPart
//mp.addBodyPart(mbp);
//}
//mailMessage.setContent(mp);
//}

//發送郵件
Transport.send(mailMessage);
returntrue;
}catch(MessagingExceptionex){
ex.printStackTrace();

}
returnfalse;
}
publicclassMailSenderInfo{
//發送郵件的伺服器的IP和埠
privateStringmailServerHost;
privateStringmailServerPort="25";
//郵件發送者的地址
privateStringfromAddress;
//郵件接收者的地址
privateStringtoAddress;
//登陸郵件發送伺服器的用戶名和密碼
privateStringuserName;
privateStringpassword;
//是否需要身份驗證
privatebooleanvalidate=false;
//郵件主題
privateStringsubject;
//郵件的文本內容
privateStringcontent;
//郵件附件的文件名
privateString[]attachFileNames;

//郵件抄送人

privateList<String>ccUserList;
/***//**
*獲得郵件會話屬性
*/
publicPropertiesgetProperties(){
Propertiesp=newProperties();
p.put("mail.smtp.host",this.mailServerHost);
p.put("mail.smtp.port",this.mailServerPort);
p.put("mail.smtp.auth",validate?"true":"false");
returnp;
}
publicStringgetMailServerHost(){
returnmailServerHost;
}
publicvoidsetMailServerHost(StringmailServerHost){
this.mailServerHost=mailServerHost;
}
publicStringgetMailServerPort(){
returnmailServerPort;
}
publicvoidsetMailServerPort(StringmailServerPort){
this.mailServerPort=mailServerPort;
}
publicbooleanisValidate(){
returnvalidate;
}
publicvoidsetValidate(booleanvalidate){
this.validate=validate;
}
publicString[]getAttachFileNames(){
returnattachFileNames;
}
publicvoidsetAttachFileNames(String[]fileNames){
this.attachFileNames=fileNames;
}
publicStringgetFromAddress(){
returnfromAddress;
}
publicvoidsetFromAddress(StringfromAddress){
this.fromAddress=fromAddress;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
publicStringgetToAddress(){
returntoAddress;
}
publicvoidsetToAddress(StringtoAddress){
this.toAddress=toAddress;
}
publicStringgetUserName(){
returnuserName;
}
publicvoidsetUserName(StringuserName){
this.userName=userName;
}
publicStringgetSubject(){
returnsubject;
}
publicvoidsetSubject(Stringsubject){
this.subject=subject;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(StringtextContent){
this.content=textContent;
}
publicList<String>getCcUserList(){
returnccUserList;
}
publicvoidsetCcUserList(List<String>ccUserList){
this.ccUserList=ccUserList;
}


}

publicstaticvoidmain(String[]args){
//這個類主要是設置郵件
MailSenderInfomailInfo=newMailSenderInfo();
mailInfo.setMailServerHost("smtp.163.com");
mailInfo.setMailServerPort("25");
mailInfo.setValidate(true);
mailInfo.setUserName("[email protected]");
mailInfo.setPassword("zzzong0828");//您的郵箱密碼
mailInfo.setFromAddress("");
//接受方信息
mailInfo.setToAddress("");
mailInfo.setSubject("郵箱標題");
mailInfo.setContent("設置郵箱內容如http://www.guihua.org中國桂花網是中國最大桂花網站==");

String[]files=newString[]{"D:/1.txt","D:/2.txt","D:/3.txt"};
mailInfo.setAttachFileNames(files);
//這個類主要來發送郵件
SimpleMailSendersms=newSimpleMailSender();
sms.sendTextMail(mailInfo);//發送文體格式
//sms.sendHtmlMail(mailInfo);//發送html格式
}



這樣發

② javamail發送帶Excel附件郵件

我這里有個可用的。也來自網路,測了下可以。。

③ 如何用java實現發郵件功能,並有幾點注意事項

packagecom.victor;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Properties;
importjavax.activation.DataHandler;
importjavax.activation.DataSource;
importjavax.activation.FileDataSource;
importjavax.mail.BodyPart;
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.Multipart;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMultipart;
importjavax.mail.internet.MimeUtility;
{
privateMimeMessagemessage;
privateSessionsession;
privateTransporttransport;
privateStringmailHost="";
privateStringsender_username="";
privateStringsender_password="";
privatePropertiesproperties=newProperties();
/*
*初始化方法
*/
publicJavaMailWithAttachment(booleandebug){
InputStreamin=JavaMailWithAttachment.class.getResourceAsStream("/MailServer.properties");
try{
properties.load(in);
this.mailHost=properties.getProperty("mail.smtp.host");
this.sender_username=properties.getProperty("mail.sender.username");
this.sender_password=properties.getProperty("mail.sender.password");
}catch(IOExceptione){
e.printStackTrace();
}

session=Session.getInstance(properties);
session.setDebug(debug);//開啟後有調試信息
message=newMimeMessage(session);
}
/**
*發送郵件
*
*@paramsubject
*郵件主題
*@paramsendHtml
*郵件內容
*@paramreceiveUser
*收件人地址
*@paramattachment
*附件
*/
publicvoiddoSendHtmlEmail(Stringsubject,StringsendHtml,StringreceiveUser,Fileattachment){
try{
//發件人
InternetAddressfrom=newInternetAddress(sender_username);
message.setFrom(from);
//收件人
InternetAddressto=newInternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO,to);
//郵件主題
message.setSubject(subject);
//向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
Multipartmultipart=newMimeMultipart();

//添加郵件正文
BodyPartcontentPart=newMimeBodyPart();
contentPart.setContent(sendHtml,"text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);

//添加附件的內容
if(attachment!=null){
BodyPartattachmentBodyPart=newMimeBodyPart();
DataSourcesource=newFileDataSource(attachment);
attachmentBodyPart.setDataHandler(newDataHandler(source));

//網上流傳的解決文件名亂碼的方法,其實用MimeUtility.encodeWord就可以很方便的搞定
//這里很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼
//sun.misc.BASE64Encoderenc=newsun.misc.BASE64Encoder();
//messageBodyPart.setFileName("=?GBK?B?"+enc.encode(attachment.getName().getBytes())+"?=");

//MimeUtility.encodeWord可以避免文件名亂碼
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentBodyPart);
}

//將multipart對象放到message中
message.setContent(multipart);
//保存郵件
message.saveChanges();
transport=session.getTransport("smtp");
//smtp驗證,就是你用來發郵件的郵箱用戶名密碼
transport.connect(mailHost,sender_username,sender_password);
//發送
transport.sendMessage(message,message.getAllRecipients());
System.out.println("sendsuccess!");
}catch(Exceptione){
e.printStackTrace();
}finally{
if(transport!=null){
try{
transport.close();
}catch(MessagingExceptione){
e.printStackTrace();
}
}
}
}
publicstaticvoidmain(String[]args){
JavaMailWithAttachmentse=newJavaMailWithAttachment(true);
System.out.println(se);
Fileaffix=newFile("E:\測試-test.txt");
//Fileaffix=null;
se.doSendHtmlEmail("##","###","####@##.com",affix);//
}
}

注意點:1 jar可能有沖突,如果是demo可以直接應用mail.jar

如果是一個工程則要替換javaEE中的mail.jar包

2 關於properties配置文件的地址問題

Class.getResourceAsStream(String path) : path 不以』/'開頭時默認是從此類所在的包下取資源,以』/'開頭則是從

ClassPath根下獲取。其只是通過path構造一個絕對路徑,最終還是由ClassLoader獲取資源。

④ 如何用java實現郵件發送

javamail
沒錯 就是這個 你看看api就能寫出來 順便說下 ,javamail群發的時候 要注意 發送的時候開個線程,等幾秒再發下一個 要不會被郵件伺服器拒絕的

⑤ java調取excel表格內容,再調用javamail api直接發送

http://www.cnblogs.com/xwdreamer/archive/2012/02/22/2363152.html poi讀取excel
http://www.blogjava.net/wangfun/archive/2009/04/15/265748.html 發送你的郵件

⑥ 如何使用Java發送qq郵件

方法:

1.前提准備工作:
首先,郵件的發送方要開啟POP3 和SMTP服務--即發送qq郵件的賬號要開啟POP3 和SMTP服務

2.開啟方法:
登陸qq郵箱
3.點擊 設置

4.點擊—-賬戶

5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 —點擊開啟

6.送簡訊 —–點擊確定

7.稍等一會,很得到一個授權碼! –注意:這個一定要記住,一會用到

8.點擊保存修改 —OK 完成

9.java 測試代碼:
package cn.cupcat.test;
import java.util.Properties;
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.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {

Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 連接協議
properties.put("mail.smtp.host", "smtp.qq.com");// 主機名
properties.put("mail.smtp.port", 465);// 埠號
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//設置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");//設置是否顯示debug信息 true 會在控制台顯示相關信息
//得到回話對象
Session session = Session.getInstance(properties);
// 獲取郵件對象
Message message = new MimeMessage(session);
//設置發件人郵箱地址
message.setFrom(new InternetAddress("[email protected]"));
//設置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("[email protected]") });
//設置郵件標題
message.setSubject("這是第一封Java郵件");
//設置郵件內容
message.setText("內容為: 這是第一封java發送來的郵件。");
//得到郵差對象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("[email protected]", "vvctybgbvvophjcj");//密碼為剛才得到的授權碼
//發送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運行就會發出郵件了。。。。
下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最後,祝你也能成功,如果有什麼問題,可以一起討論!

注意事項

得到的授權碼一定要保存好,程序中要使用

⑦ 如何用java實現發送html格式的郵件

首先Java發送郵件需要用到JavaMail,先到Oracle官網上下載好最新版本的JavaMail(剛才看了一下,最新是1.5.3),把下載的這個jar文件放到classpath里(如果是Web項目,就放到WEB-INF/lib目錄下。

JavaMail主要支持發送純文本的和html格式的郵件。

發送html格式的郵件的一個常式如下:

importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeUtility;
importjavax.mail.Session;
importjavax.mail.MessagingException;
importjavax.mail.Transport;

publicclassSendHtmlMail{
publicstaticvoidsendMessage(StringsmtpHost,
Stringfrom,Stringto,
Stringsubject,StringmessageText)
throwsMessagingException,java.io.UnsupportedEncodingException{

//Step1:Configurethemailsession
System.out.println("Configuringmailsessionfor:"+smtpHost);
java.util.Propertiesprops=newjava.util.Properties();
props.setProperty("mail.smtp.auth","true");//指定是否需要SMTP驗證
props.setProperty("mail.smtp.host",smtpHost);//指定SMTP伺服器
props.put("mail.transport.protocol","smtp");
SessionmailSession=Session.getDefaultInstance(props);
mailSession.setDebug(true);//是否在控制台顯示debug信息

//Step2:Constructthemessage
System.out.println("Constructingmessage-from="+from+"to="+to);
InternetAddressfromAddress=newInternetAddress(from);
InternetAddresstoAddress=newInternetAddress(to);

MimeMessagetestMessage=newMimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
testMessage.setSentDate(newjava.util.Date());
testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));

testMessage.setContent(messageText,"text/html;charset=gb2312");
System.out.println("Messageconstructed");

//Step3:Nowsendthemessage
Transporttransport=mailSession.getTransport("smtp");
transport.connect(smtpHost,"webmaster","password");
transport.sendMessage(testMessage,testMessage.getAllRecipients());
transport.close();

System.out.println("Messagesent!");
}

publicstaticvoidmain(String[]args){

StringsmtpHost="localhost";
Stringfrom="[email protected]";
Stringto="[email protected]";
Stringsubject="html郵件測試";//subjectjavamail自動轉碼

StringBuffertheMessage=newStringBuffer();
theMessage.append("<h2><fontcolor=red>這倒霉孩子</font></h2>");
theMessage.append("<hr>");
theMessage.append("<i>年年失望年年望</i>");
try{
SendHtmlMail.sendMessage(smtpHost,from,to,subject,theMessage.toString());
}
catch(javax.mail.MessagingExceptionexc){
exc.printStackTrace();
}
catch(java.io.){
exc.printStackTrace();
}
}
}


JavaMail是封裝了很多郵件操作的,所以使用起來不很困難,建議你到JavaMail官網看一下API或下載Java Doc API文檔。

⑧ java 發送郵件

要兩個java文件 還有一個mail.jar是不是只能用javamail誰也不敢說

第一個:

public class Constant {

public static final String mailAddress ="用戶名@163.com";
public static final String mailCount ="用戶名";
public static final String mailPassword ="密碼";
public static final String mailServer ="smtp.163.com";
//pukeyouxintest,

}

第二個:

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {
/**
* 發送簡單郵件
* @param str_from:發件人地址
* @param str_to:收件人地址
* @param str_title:郵件標題
* @param str_content:郵件正文
*/
public static void send(String str_from,String str_to,String str_title,String str_content) {

// str_content="<a href='www.163.com'>html元素</a>"; //for testing send html mail!

try {
//建立郵件會話
Properties props=new Properties(); //用來在一個文件中存儲鍵-值對的,其中鍵和值是用等號分隔的,
//存儲發送郵件伺服器的信息
props.put("mail.smtp.host",Constant.mailServer);
//同時通過驗證
props.put("mail.smtp.auth","true");
//根據屬性新建一個郵件會話
Session s=Session.getInstance(props);
s.setDebug(true); //有他會列印一些調試信息。

//由郵件會話新建一個消息對象
MimeMessage message=new MimeMessage(s);

//設置郵件
InternetAddress from= new InternetAddress(str_from); //[email protected]
message.setFrom(from); //設置發件人的地址
//
// //設置收件人,並設置其接收類型為TO
InternetAddress to=new InternetAddress(str_to); //[email protected]
message.setRecipient(Message.RecipientType.TO, to);

//設置標題
message.setSubject(str_title); //java學習

//設置信件內容
// message.setText(str_content); //發送文本郵件 //你好嗎?
message.setContent(str_content, "text/html;charset=gbk"); //發送HTML郵件 //<b>你好</b><br><p>大家好</p>
//設置發信時間
message.setSentDate(new Date());

//存儲郵件信息
message.saveChanges();

//發送郵件
Transport transport=s.getTransport("smtp");
//以smtp方式登錄郵箱,第一個參數是發送郵件用的郵件伺服器SMTP地址,第二個參數為用戶名,第三個參數為密碼
transport.connect(Constant.mailServer,Constant.mailCount,Constant.mailPassword);
//發送郵件,其中第二個參數是所有已設好的收件人地址
transport.sendMessage(message,message.getAllRecipients());
transport.close();

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
//測試用的,你吧你想寫的內容寫上去就行
send(Constant.mailAddress,"收件人郵箱","標題","<b>內容</b>");
}
}

然後把mail.jar導入,就可以了,我用的是163 的,其他的吧相應的伺服器改一下就行了

⑨ 怎樣用java發送郵件

  1. 首先下載 JavaMail jar 包,並導入到項目中。下載地址

  2. 編寫代碼,代碼如下:

  3. importjavax.mail.Authenticator;
    importjavax.mail.Message;
    importjavax.mail.MessagingException;
    importjavax.mail.PasswordAuthentication;
    importjavax.mail.Session;
    importjavax.mail.Transport;
    importjavax.mail.internet.AddressException;
    importjavax.mail.internet.InternetAddress;
    importjavax.mail.internet.MimeMessage;

    publicclassApp45{

    publicstaticvoidmain(String[]args)throwsAddressException,MessagingException{

    Propertiesproperties=System.getProperties();

    properties.setProperty("mail.smtp.host","郵件發送伺服器");

    properties.setProperty("mail.smtp.auth","true");

    Sessionsession=Session.getDefaultInstance(properties,newAuthenticator(){
    @Override
    (){

    //設置發件人郵件帳號和密碼
    ("郵件帳號","密碼");
    }
    });

    MimeMessagemessage=newMimeMessage(session);

    //設置發件人郵件地址
    message.setFrom(newInternetAddress("發件人郵件地址"));

    //設置收件人郵件地址
    message.addRecipient(Message.RecipientType.TO,newInternetAddress("收件人郵件地址"));

    message.setSubject("這里是郵件主題。");

    message.setText("這里是郵件內容。");

    Transport.send(message);
    }
    }
閱讀全文

與java發送郵件表格相關的資料

熱點內容
山西有什麼app 瀏覽:406
app怎麼樣購買內存 瀏覽:30
如何注冊sqlserver伺服器 瀏覽:76
上士命令 瀏覽:490
股市中帶星號的app是什麼 瀏覽:709
什麼路由可以刷機做列印機伺服器 瀏覽:7
電腦怎麼找到雲伺服器 瀏覽:871
微信怎麼發應用app 瀏覽:776
花生殼dns伺服器地址 瀏覽:648
squad伺服器一般什麼時候人多 瀏覽:479
程序員戰門課 瀏覽:474
config保存伺服器地址 瀏覽:317
預訂網吧座位的app叫什麼 瀏覽:416
香港伺服器主機地址 瀏覽:640
網店美工pdf 瀏覽:447
一堆文件夾怎麼弄出來 瀏覽:743
博途如何編譯硬體 瀏覽:418
fortran程序pdf 瀏覽:504
電池消耗演算法 瀏覽:394
伺服器中斷連接怎麼處理 瀏覽:222