怎么用HTML格式发送邮件.既怎么发HTML格式的邮件..?

2024-12-14 08:39:32
推荐回答(2个)
回答1:

首先,我们来说一下如何发送HTML邮件。

发送图文HTML邮件很简单,发送方法如下:

首先,复制邮件代码,然后打开自己的邮箱,转换为代码输入方式,粘贴代码,就可以发送邮件了。详见下图(图1为QQ邮箱,图2为163邮箱)。

接下来,我们再说一下如何制作图文HTML邮件。

制作图文邮件如果是自己写代码,那就太难了,反正有很多坑,如果不是专业人员,就不要自己写了。

难道没有其他办法了,有的,那就是使用别人做好的现成的平台来写HTML邮件,跳过写代码这一步,这样做邮件就很简单了,可以说人人都会。具体怎么做,请自行百度搜索“图文并茂+邮件”,就能找到自己的制作方法了。


附:相关概念:

什么是html格式邮件?

HTML邮件就是使用HTML标记来写的邮件。

就邮件类别来说,纯文本邮件和HTML邮件没有区别,都是邮件,但是后者内容和形式更加丰富,可以有颜色、链接和图片,就象一个彩色的图文彩页,表现力更强。

就本质来说,HTML实际上它就是一个网页,但又不同于网页,因HTML邮件要求兼容性必须更好,否则就有可能存在错位无法观看的风险。

那么HTML制作是不是很难呢?从专业写代码的角度说,确实艰难,代码、兼容性、各种邮箱的差异性均有可能造成邮件乱码和错位,达不到展示效果。

实际上难不难关键在于你的着眼点,在于你的解决问题的方法。如果你的方法使用正确了,HTML邮件的制作又是艰简单的,因为他们都跳过了写代码,所以适合普通人使用,网上有很多类似的平台,请自行百度搜索“图文并茂+邮件”就会找到适合自己的制作图文邮件的方法,提高制作效率。







回答2:

发送html格式的邮件void jButton8_actionPerformed(ActionEvent e) {
try
{//此段代码用来发送带有html信息的邮件
Properties props = new Properties();
Authenticator auth = new Email_Autherticator();
props.put( "mail.smtp.host ",host);
props.put( "mail.smtp.auth ", "true ");
Session session = Session.getDefaultInstance(props,auth);
MimeMessage message = new MimeMessage(session);
//设置邮件正文
String htmltext= "

hello

"+ " click me please! ";
message.setContent(htmltext, "text/html ");//text/plain设置邮件的格式因为是带有html文档的,所以用text/html格式

message.setSubject(mail_subject);
// message.setText(mail_body);在使用message.setContent()时候,好象不能用message.setText()
message.setHeader(mail_head_name,mail_head_value);
message.setSentDate(new Date());
Address address = new InternetAddress(mail_from, "sunxiaoming ");
message.setFrom(address);
Address toAddress = new InternetAddress(mail_to);
message.addRecipient(Message.RecipientType.TO,toAddress);
Transport.send(message);
System.out.println( "send ok! ");
}
catch(Exception ex)
{
System.out.println( "faild "+ex);
}

}

void jButton9_actionPerformed(ActionEvent e) {
try
{//此段代码用来发送带有图片的邮件
String file = "./20020423/test.gif ";
Properties props = new Properties();
Authenticator auth = new Email_Autherticator();

props.put( "mail.smtp.host ",host);
props.put( "mail.smtp.auth ", "true ");
Session session = Session.getDefaultInstance(props,auth);
MimeMessage message = new MimeMessage(session);

message.setSubject( "Embedded Image ");
Address address = new InternetAddress(mail_from, "sunxiaoming ");
message.setFrom(address);
Address toAddress = new InternetAddress(mail_to);
message.addRecipient(Message.RecipientType.TO,toAddress);
message.setHeader(mail_head_name,mail_head_value);
message.setSentDate(new Date());

BodyPart messageBodyPart = new MimeBodyPart();
String htmlText = "

"+ " ";//加入图片
messageBodyPart.setContent(htmlText, "text/html ");
//原理同发送附件一样,将邮件看成有多个部分组成的,先设置每一个部分,然后再全部将它们合起来即可。
//Create a related multi-part to combine the parts
MimeMultipart multipart = new MimeMultipart( "related ");
multipart.addBodyPart(messageBodyPart);

//Create part for the image
messageBodyPart = new MimeBodyPart();

//Fetch the image and associate to part
DataSource fds = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(fds));
messageBodyPart.setHeader( "Content-ID ", "memememe ");

//Add part to multi-part
multipart.addBodyPart(messageBodyPart);

//Associate multi-part with message
message.setContent(multipart);

Transport.send(message);
System.out.println( "send ok! ");
}
catch(Exception ex)
{
System.out.println( "faild "+ex);
}

}

void jButton7_actionPerformed(ActionEvent e) {
try
{//该程序用来接受带有html信息的邮件
Properties props = System.getProperties();
Authenticator auth = new Email_Autherticator();
props.put( "mail.smtp.host ",host);
props.put( "mail.smtp.auth ", "true ");
Session session = Session.getDefaultInstance(props,auth);
Store store = session.getStore( "pop3 ");
store.connect(host,username,password);

//After connecting to the Store,you can get a Folder,which must be opened before you can read messages from it:
Folder folder = store.getFolder( "INBOX ");
folder.open(Folder.READ_WRITE);
Message message[] = folder.getMessages();

for (int i=0,n=message.length;i // for (int i=0,n=1;i {
String out_from_person = ((InternetAddress)message[i].getFrom()[0]).getPersonal();
String out_from_address = ((InternetAddress)message[i].getFrom()[0]).getAddress();
System.out.println( "From: "+out_from_person+ "\t ");
System.out.println( "Address: "+out_from_address+ "\t ");

String out_subject = message[i].getSubject();
System.out.println( "Subject: "+out_subject+ "\t ");

//获取邮件的信息,并且对其每一部分进行操作
Part messagePart = message[i];
Object out_content = messagePart.getContent();
if (out_content instanceof Multipart)
{
messagePart = ((Multipart)out_content).getBodyPart(0);
System.out.println( "[ Multipart Message ] ");
}
String out_content_type = messagePart.getContentType();
System.out.println( "CONTENT: "+out_content_type);

jEditorPane1.setText( " ");//将其显示在jEditorPanel上
jEditorPane1.setContentType( "text/html ");
jEditorPane1.setText((String)out_content);
jEditorPane1.setEditable(false);

/*
if (out_content_type.startsWith( "text/plain ") || out_content_type.startsWith( "text/html "))
{
InputStream ipstm = messagePart.getInputStream();
BufferedReader bufreader = new BufferedReader(new InputStreamReader(ipstm));
String thisLine = bufreader.readLine();
while (thisLine != null)
{
System.out.println( "thisLine: "+thisLine);
thisLine = bufreader.readLine();
}
}
*/
System.out.println( "------------------------------------------------------------ ");
message[i].setFlag(Flags.Flag.DELETED,true);
}
folder.close(true);
store.close();
}
catch(Exception ej2)
{
System.out.println(ej2);
}

}