Ⅰ java發送html郵件的時候外嵌css樣式表怎麼處理
貌似沒有辦法,只能通過拼接到html裡面
Ⅱ java發送html模版的郵件
你可以找一封完整的廣告郵件,然後轉發,接著選擇代碼模式查看,你就可以修改廣告的內容
Ⅲ java發送html格式郵件是否能有js
不要帶js的,如果是想用HTML做發送的內容及布局的話,那就不要用js。
如果你是在發送郵件的時候將HTML文件作為附件的話,那是可以有js的。
Ⅳ ·[急急急」如何在java程序裡面發送html郵件(在線)
上個實例吧,希望能幫助到你,maven裡面引入javamail
在CODE上查看代碼片派生到我的代碼片
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
然後寫個簡單的工具類
在CODE上查看代碼片派生到我的代碼片
packagecom.xxx.tools;
importjavax.mail.MessagingException;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeUtility;
publicclassHtmlEmail{
privateStringsmtpServer;
privateStringusername;
privateStringpassword;
publicvoidsendMessage(Stringto,Stringsubject,StringmessageText)throwsMessagingException,java.io.UnsupportedEncodingException{
java.util.Propertiesprops=newjava.util.Properties();
props.setProperty("mail.smtp.auth","true");//指定是否需要SMTP驗證
props.setProperty("mail.smtp.host",smtpServer);//指定SMTP伺服器
props.put("mail.transport.protocol","smtp");
SessionmailSession=Session.getDefaultInstance(props);
//mailSession.setDebug(true);//是否在控制台顯示debug信息
InternetAddressfromAddress=newInternetAddress(username);
InternetAddresstoAddress=newInternetAddress(to);
MimeMessageemailMessage=newMimeMessage(mailSession);
emailMessage.setFrom(fromAddress);
emailMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
emailMessage.setSentDate(newjava.util.Date());
emailMessage.setSubject(MimeUtility.encodeText(subject,"utf-8","B"));
emailMessage.setContent(messageText,"text/html;charset=utf-8");
Transporttransport=mailSession.getTransport("smtp");
transport.connect(smtpServer,username,password);
transport.sendMessage(emailMessage,emailMessage.getAllRecipients());
transport.close();
}
publicStringgetSmtpServer(){
returnsmtpServer;
}
publicvoidsetSmtpServer(StringsmtpServer){
this.smtpServer=smtpServer;
}
publicStringgetUsername(){
returnusername;
}
publicvoidsetUsername(Stringusername){
this.username=username;
}
publicStringgetPassword(){
returnpassword;
}
publicvoidsetPassword(Stringpassword){
this.password=password;
}
}
然後在spring裡面配置一下
在CODE上查看代碼片派生到我的代碼片
<beanid="htmlMail"class="com.xxx.tools.HtmlEmail">
<propertyname="smtpServer"value="smtp.qq.com"/>
<propertyname="username"value="[email protected]"></property>
<propertyname="password"value="xxxxxxx"></property>
</bean>
然後在需要使用的地方使用:
在CODE上查看代碼片派生到我的代碼片
@Controller
@RequestMapping("/user")
publicclassUserRegController{
@Resource
privateHtmlEmailemail;
@RequestMapping("/reg")
publicvoidsendMail(@RequestParam("username")Stringusername,@RequestParam("userpass")Stringuserpass,@RequestParam("email")Stringemail_addr,HttpServletRequestrequest){
Stringvalid_string=request.getSession(true).getId();
Stringvalid_url="<ahref='http://localhost:8080/user/valid?username="+username+"&validString="+valid_string+"'target='_blank'>點擊此鏈接激活賬號</a>";
try{
email.sendMessage(email_addr,"用戶激活",valid_url);
}catch(MessagingExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
}
Ⅳ java+html,點擊按扭,獲取驗證碼並發送至郵箱
用ajax觸發,點擊按鈕後,ajax請求後台的某個.do或action,讓後再該controller中調用你寫的兩個工具類,生成驗證碼並發送到郵箱,對了還需要把生成的驗證碼放到session裡面,方便後續的驗證碼校驗。
Ⅵ java發送html格式郵件是否能有js
1、如果是想用HTML做發送的內容及布局的話,那就不要用js
2、如果在發送郵件的時候將HTML文件作為附件的話,那是可以有js的
Java發送郵件需要用到JavaMail,先到Oracle官網上下載好最新版本的JavaMail(剛才看了一下,最新是1.5.3),把下載的這個jar文件放到classpath里(如果是Web項目,就放到WEB-INF/lib目錄下。
JavaMail主要支持發送純文本的和html格式的郵件。
java發送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();
}
}
}
Ⅶ java發送html模版的郵件
setContent(參數),裡面的字元串要是完成的html代碼,"<html><head></head><body></body></html>",這種,我只寫了個大概,反正就是要是完整的html代碼,祝你成功
Ⅷ 如何在java程序裡面發送html郵件
把下載的這個jar文件放到classpath里(如果是Web項目,就放到WEB-INF/lib目錄下。 JavaMail主要支持發送純文本的和html格式的郵件...