我正在尝试添加内联链接(#链接到HTML正文中的内联表),我可以在我的电子邮件中导航。
我正在使用javax.mail
API和text/html
。
问题是当链接在Outlook等电子邮件客户端中打开时,链接不起作用。
如果邮件是在浏览器中打开的(从电子邮件客户端的浏览器选项'中的'视图),那么链接就可以正常工作。
请协助解决此问题(如何在邮件客户端中使用内联链接)。 下面是我用来发送邮件的课程。
import java.io.UnsupportedEncodingException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.afl.crawler.exceptions.CrawlerException;
public class EmailTestClass {
private static StringBuilder message = new StringBuilder();
public static void main(String[] args) throws CrawlerException {
LocalDate today = LocalDate.now();
List<LocalDate> dateList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
dateList.add(today.minusDays(i));
}
configureAndSendMail(dateList);
}
private static void configureAndSendMail(List<LocalDate> dateList) throws CrawlerException {
try {
Properties smtpProps = new Properties();
smtpProps.put("mail.smtp.starttls.enable", "true");
smtpProps.put("mail.smtp.host", "smtp.gmail.com");
smtpProps.put("mail.smtp.port", "587");
smtpProps.put("mail.smtp.auth", "true");
Authenticator auth = new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("me@gmail.com", "myP@ssword");
}
};
Session session = Session.getInstance(smtpProps, auth);
Message msg = new MimeMessage(session);
InternetAddress[] toAddrs = { new InternetAddress("someone@example.com") };
msg.setFrom(new InternetAddress("me@gmail.com", "Test Account"));
msg.setRecipients(Message.RecipientType.TO, toAddrs);
msg.setSubject("Test EMail Links");
msg.setSentDate(new Date());
msg.setContent(getMailContent(dateList), "text/html; charset=utf-8");
Transport.send(msg);
} catch (MessagingException | UnsupportedEncodingException ex) {
throw new CrawlerException("Failed to process Email", ex);
}
}
private static Object getMailContent(List<LocalDate> dateList) {
message.append("<html><head></head><body bgcolor=\"#E1E1E1\" leftmargin=\"0\" marginwidth=\"0\" topmargin=\"0\" marginheight=\"0\" offset=\"0\"><center>");
// Prepare the Navigation links
message.append("<div style=\"width: 200px;\"><h2>Navigation Links:</h2><ul>");
dateList.forEach(date -> {
message.append("<li><a href=\"#")
.append(date.toEpochDay())
.append("\" target=\"_self\">")
.append(date.format(DateTimeFormatter.ofPattern("dd MMMM yyyy")))
.append("</a></li>");
});
message.append("</ul></div>");
// Prepare the data Tables
builddataTables(dateList);
message.append("</center></body></html>");
return message.toString();
}
private static void builddataTables(List<LocalDate> dateList) {
dateList.forEach(date -> {
message.append("<table ")
.append("id=\"")
.append(date.toEpochDay())
.append("\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:500px;background-color: #cedbf3;border: 1px solid rgba(19, 14, 14, 0.56);padding:15px;\"><tr><td colspan=\"2\" style=\"background-color: #5ab4ff;color:#ffffff;line-height:100%;font-family:Helvetica,Arial,sans-serif;font-size:20px;font-weight:400;margin-bottom:5px;text-align:center;\">")
.append(date.format(DateTimeFormatter.ofPattern("cccc, dd MMMM yyyy")))
.append("</td></tr><tr><td style=\"width:30%;\">Key Dat1</td><td style=\"width:70%;\">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac mi eu lorem sagittis lacinia vel ac metus</td></tr><tr><td>Key Data2</td><td>Cras lobortis consectetur mi a porttitor. Vestibulum blandit urna et elit sollicitudin</td></tr></table>");
});
}
}
答案 0 :(得分:1)
按照Aaron指出的链接,我将详细信息表包含在一个命名的锚标记中。
它已经在 Gmail 和 Outlook 2016 客户端上进行了测试,并且正在按预期运行。
现在builddataTables
方法实现如下。
private static void builddataTables(List<LocalDate> dateList) {
dateList.forEach(date -> {
message.append("<a name=\"")
.append(date.toEpochDay())
.append("\" id=\"")
.append(date.toEpochDay())
.append("\"><table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" style=\"width:500px;background-color: #cedbf3;border: 1px solid rgba(19, 14, 14, 0.56);padding:15px;\"><tr><td colspan=\"2\" style=\"background-color: #5ab4ff;color:#ffffff;line-height:100%;font-family:Helvetica,Arial,sans-serif;font-size:20px;font-weight:400;margin-bottom:5px;text-align:center;\">")
.append(date.format(DateTimeFormatter.ofPattern("cccc, dd MMMM yyyy")))
.append("</td></tr><tr><td style=\"width:30%;\">Key Dat1</td><td style=\"width:70%;\">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent ac mi eu lorem sagittis lacinia vel ac metus</td></tr><tr><td>Key Data2</td><td>Cras lobortis consectetur mi a porttitor. Vestibulum blandit urna et elit sollicitudin</td></tr></table></a>");
});
}