search
HomeJavaJavaBaseGarbled characters appear when sending emails via java mail

Garbled characters appear when sending emails via java mail

Nov 25, 2019 am 09:27 AM
javamailGarbled charactersmail

Garbled characters appear when sending emails via java mail

1. Specify the text encoding when sending the text:

Use

MimeBodyPart  body = new MimeBodyPart();
body.setContent(content, "text/html;charset=GB2312");

when sending the email. Pay attention to the The content encoding must be the specified encoding format.

2. When setting the email title, you must also specify the encoding of the title:

MimeMultipart mmp=new MimeMultipart();
mmp.setSubject(subject, "GB2312");

The same as above also requires that the encoding of the subject is consistent with the specified encoding.

3. You can also specify the transmission encoding in the header when sending the text:

body.setHeader("Content-Transfer-Encoding", "base64"); // 指定使用base64编码

4. The example is as follows:

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class MailSender {
    public static void main(String[] args) {
        try {
     String host = "staff.tixa.com"; // smtp主机
     String username = "sample@staff.tixa.com"; // 认证用户名
     String password = "sample"; // 认证密码
     String from = "例子<sample@staff.tixa.com>"; // 发送者
     String to = "toOne@staff.tixa.com, toAnother@staff.tixa.com"; // 接受者,用“,”分隔
     String subject = "测试例子";
     String content = "仅仅是个供测试的例子。";
     // 建立session
     Properties prop = new Properties();
     prop.put("mail.smtp.host", host);
     prop.put("mail.smtp.auth", "true"); //是否需要认证
     Session session = Session.getDefaultInstance(prop, null);
     // 创建MIME邮件对象
     MimeMessage mimeMsg = new MimeMessage(session);
     MimeMultipart mp = new MimeMultipart();
     // 设置信息
     mimeMsg.setFrom(new InternetAddress(from));
     mimeMsg.setSubject(subject, "GB2312"); // !!!注意设置编码
     mimeMsg.setRecipients(
         Message.RecipientType.TO,
  InternetAddress.parse(to));
     // 设置正文
     BodyPart body = new MimeBodyPart();
     body.setContent(content, "text/plain;charset=GB2312"); // !!!注意设置编码
     mp.addBodyPart(body);
     mimeMsg.setContent(mp);
     // 发送邮件
     Transport transport = session.getTransport("smtp");
     transport.connect(host, username, password);
     transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
     transport.close();
 }
 catch(Exception exp) {
     exp.printStackTrace();
 }
    }

Recommended tutorial :java introductory tutorial

The above is the detailed content of Garbled characters appear when sending emails via java mail. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.