Ⅰ 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格式的邮件...