這里是初學者,但我使用此示例發送 java 電子郵件邀請。如果您查看該鏈接中的最后一個示例,當用戶創建會議時,我在 Spring CRUD 應用程式中作業,會發送一封包含會議詳細資訊的電子郵件,一切都很好。在資料庫中輸入的日期時間是正確的,但是會議邀請總是比輸入的晚 1 小時。我知道它與 TimeZones 有關,但查看代碼我不知道從哪里開始。那么我必須更改什么才能為德國柏林設定夏令時?
繼承人的代碼:
public class Index {
public static void main(String[] args) {
try {
final String username = "[email protected]";
final String password = "xxxxx";
String from = "[email protected]";
String to = "[email protected]";
String subject = "Meeting Subject";
String startDate = "20201208"; // Date Formate: YYYYMMDD
String endDate = "20201208"; // Date Formate: YYYYMMDD
String startTime = "0400"; // Time Formate: HHMM
String endTime = "0600"; // Time Formate: HHMM
String emailBody = "Hi Team, This is meeting description. Thanks";
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
MimeMessage message = new MimeMessage(session);
message.addHeaderLine("method=REQUEST");
message.addHeaderLine("charset=UTF-8");
message.addHeaderLine("component=VEVENT");
message.setFrom(new InternetAddress(from, "New Outlook Event"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
StringBuffer sb = new StringBuffer();
StringBuffer buffer = sb.append("BEGIN:VCALENDAR\n"
"PRODID:-//Microsoft Corporation//Outlook 9.0 MIMEDIR//EN\n"
"VERSION:2.0\n"
"METHOD:REQUEST\n"
"BEGIN:VEVENT\n"
"ATTENDEE;ROLE=REQ-PARTICIPANT;RSVP=TRUE:MAILTO:" to "\n"
"DTSTART:" startDate "T" startTime "00Z\n"
"DTEND:" endDate "T" endTime "00Z\n"
"LOCATION:Conference room\n"
"TRANSP:OPAQUE\n"
"SEQUENCE:0\n"
"UID:040000008200E00074C5B7101A82E00800000000002FF466CE3AC5010000000000000000100\n"
" 000004377FE5C37984842BF9440448399EB02\n"
"CATEGORIES:Meeting\n"
"DESCRIPTION:" emailBody "\n\n"
"SUMMARY:Test meeting request\n"
"PRIORITY:5\n"
"CLASS:PUBLIC\n"
"BEGIN:VALARM\n"
"TRIGGER:PT1440M\n"
"ACTION:DISPLAY\n"
"DESCRIPTION:Reminder\n"
"END:VALARM\n"
"END:VEVENT\n"
"END:VCALENDAR");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setHeader("Content-Class", "urn:content-classes:calendarmessage");
messageBodyPart.setHeader("Content-ID", "calendar_message");
messageBodyPart.setDataHandler(new DataHandler(
new ByteArrayDataSource(buffer.toString(), "text/calendar")));// very important
// Create a Multipart
Multipart multipart = new MimeMultipart();
// Add part one
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// send message
Transport.send(message);
System.out.println("Email sent!");
} catch (MessagingException me) {
me.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
uj5u.com熱心網友回復:
問題是兩者DTSTART都DTEND被固定到UTC(因為它們以Z字符結尾)。
這是原始 RFC的摘錄:
Example: The following represents July 14, 1997, at 1:30 PM in New
York City in each of the three time formats, using the "DTSTART"
property.
DTSTART:19970714T133000 ; Local time
DTSTART:19970714T173000Z ; UTC time
DTSTART;TZID=America/New_York:19970714T133000
; Local time and time
; zone reference
以下是本地和時區提供的開始日期的一些 Java 代碼:
final var start = ZonedDateTime.of(LocalDate.of(2020, Month.DECEMBER, 8), LocalTime.of(4, 0), ZoneId.of("Europe/Berlin"));
final var startDateString = DateTimeFormatter.ofPattern("yyyyMMdd'T'HHmmss").format(start);
// local date
System.out.println(String.format("DTSTART:%s%n", startDateString));
// timezone specified
System.out.println(String.format("DTSTART;TZID=%s:%s%n", start.getZone().getId(), startDateString));
免責宣告:我沒有測驗過上述內容,但它提供了一個起點。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/530348.html
