1、如果是想用HTML做发送的内容及布局的话,那就不要用js
2、如果在发送邮件的时候将HTML文件作为附件的话,那是可以有js的
Java发送邮件需要用到JavaMail,先到Oracle官网上下载好最新版本的JavaMail(刚才看了一下,最新是1.5.3),把下载的这个jar文件放到classpath里(如果是Web项目,就放到WEB-INF/lib目录下。
JavaMail主要支持发送纯文本的和html格式的邮件。
java发送html格式邮件的具体实现:
import javax.mail.internet.InternetAddressimport javax.mail.internet.MimeMessage
import javax.mail.internet.MimeUtility
import javax.mail.Session
import javax.mail.MessagingException
import javax.mail.Transport
public class SendHtmlMail {
public static void sendMessage(String smtpHost,
String from, String to,
String subject, String messageText)
throws MessagingException,java.io.UnsupportedEncodingException {
// Step 1: Configure the mail session
System.out.println("Configuring mail session for: " + smtpHost)
java.util.Properties props = new java.util.Properties()
props.setProperty("mail.smtp.auth", "true")//指定是否需要SMTP验证
props.setProperty("mail.smtp.host", smtpHost)//指定SMTP服务器
props.put("mail.transport.protocol", "smtp")
Session mailSession = Session.getDefaultInstance(props)
mailSession.setDebug(true)//是否在控制台显示debug信息
// Step 2: Construct the message
System.out.println("Constructing message - from=" + from + " to=" + to)
InternetAddress fromAddress = new InternetAddress(from)
InternetAddress toAddress = new InternetAddress(to)
MimeMessage testMessage = new MimeMessage(mailSession)
testMessage.setFrom(fromAddress)
testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress)
testMessage.setSentDate(new java.util.Date())
testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"))
testMessage.setContent(messageText, "text/htmlcharset=gb2312")
System.out.println("Message constructed")
// Step 3: Now send the message
Transport transport = mailSession.getTransport("smtp")
transport.connect(smtpHost, "webmaster", "password")
transport.sendMessage(testMessage, testMessage.getAllRecipients())
transport.close()
System.out.println("Message sent!")
}
public static void main(String[] args) {
String smtpHost = "localhost"
String from = "webmaster@mymail.com"
String to = "mfc42d@sohu.com"
String subject = "html邮件测试" //subject javamail自动转码
StringBuffer theMessage = new StringBuffer()
theMessage.append("<h2><font color=red>这倒霉孩子</font></h2>")
theMessage.append("<hr>")
theMessage.append("<i>年年失望年年望</i>")
try {
SendHtmlMail.sendMessage(smtpHost, from, to, subject, theMessage.toString())
}
catch (javax.mail.MessagingException exc) {
exc.printStackTrace()
}
catch (java.io.UnsupportedEncodingException exc) {
exc.printStackTrace()
}
}
}
JS是在浏览器上解析执行的,所以不能直接发邮件。只能调用客户端的outlook软件并填写内容,由访客手动发出。 当然也可以通过JS利用xmlhttp把内容发送给一个动态页,比如ASP页面,然后由ASP调用jmail组件发送邮件。代码较长,去搜索吧,我不写了。<divclass="left-wrap">
<formid="myForm"target="_blank"action="https://formspree.io/1131844379@qq.com"method="post">
<!--左上-->
<divclass="left-top">
<divclass="input-group">
<inputtype="text"name="name"class="input__fieldinput-carrier">
<labelclass="input__label"for="input-1">
<spanclass="input__label-contentinput__label-content--nao">您的姓名</span>
</label>
</div>
<divclass="input-groupinput-group-r">
<inputtype="email"name="eamil"class="input__fieldinput-carrier"type="text">
<labelclass="input__label"for="input-1">
<spanclass="input__label-contentinput__label-content--nao">您的邮箱</span>
</label>
</div>
</div>
<!--左下-->
<divclass="send-content">
<textareaname="message"class="contact-arearequired"placeholder="您的建议..."></textarea>
<buttonclass="btn-send"type="submit"id="submit"name="submit">
<span>发送</span>
<iclass="fafa-send"></i>
</button>
</div>
</form>
</div>
JS(判断内容不能为空才提交表单)
$(document).on('click','#submit',function(evt){
if($(".contact-area").val()==""){
$(".contact-area").val("您的建议不能为空!")
evt.preventDefault()//阻止提交表单到第三方网站去
}
})
扩展资料
JavaScript常用的表单交互
在form元素上使用onsubmit事件,针对表单进行拦截,返回true才能提交
<body>
<formid="myForm"onsubmit="returnvalidate()">
请输入email地址:<inputtype="text"name="email"id="email"value=""onblur="returnvalidate()">
<spanid="msg"></span>
<div><buttontype="submit">显示邮件</button></div>
</form>
</body>