java 邮件 支持html代码,Java发送带html标签内容的邮件实例代码

如下所示:

package test;

import javax.mail.internet.InternetAddress;

import 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(false);// 是否在控制台显示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/html;charset=gb2312");

System.out.println("Message constructed");

// Step 3: Now send the message

Transport transport = mailSession.getTransport("smtp");

transport.connect(smtpHost, "riteng_mes", "ri-teng1234");

transport.sendMessage(testMessage, testMessage.getAllRecipients());

transport.close();

System.out.println("Message sent!");

}

public static void main(String[] args) {

String smtpHost = "10.131.119.36";

String from = "Riteng_Mes@casetekcorp.com";

String to = "Qiang1_Zhang@intra.casetekcorp.com";

String subject = "html邮件测试"; // subject javamail自动转码

StringBuffer theMessage = new StringBuffer();

theMessage.append("

这倒霉孩子

");

theMessage.append("


");

theMessage.append("年年失望年年望");

theMessage.append("

aaabbb
cccddd
");

try {

SendHtmlMail.sendMessage(smtpHost, from, to, subject,theMessage.toString());

} catch (javax.mail.MessagingException exc) {

exc.printStackTrace();

} catch (java.io.UnsupportedEncodingException exc) {

exc.printStackTrace();

}

}

}

以上就是小编为大家带来的Java发送带html标签内容的邮件实例代码全部内容了,希望大家多多支持我们~

时间: 2016-11-09

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java发送Basic Auth认证的HTTP POST请求的示例代码: ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.Base64; public class HttpPostWithBasicAuth { public static void main(String[] args) throws IOException { String url = "http://example.com/api/resource"; String username = "myusername"; String password = "mypassword"; String postData = "param1=value1&param2=value2"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // set request method con.setRequestMethod("POST"); // set basic authentication header String auth = username + ":" + password; byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8)); String authHeaderValue = "Basic " + new String(encodedAuth); con.setRequestProperty("Authorization", authHeaderValue); // set post data con.setDoOutput(true); con.getOutputStream().write(postData.getBytes("UTF-8")); // get response int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // print response System.out.println("Response code: " + responseCode); System.out.println("Response body: " + response.toString()); } } ``` 在上面的代码中,我们使用 `HttpURLConnection` 类来建立HTTP连接,并设置了请求头中的基本认证信息。我们还设置了POST数据,并获取了响应,最后打印了响应码和响应体。请注意,此示例仅用于演示目的,实际使用中需要根据实际情况进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值