发送MS Outlook会议请求

时间:2013-05-16 10:02:58

标签: java outlook

我想发送Outlook会议,但它无法正常工作 当我运行我的代码时它会告诉我这个问题

com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. ay7sm2589668wib.9 - gsmtp

这是我的发送类代码

public class SendMailTest
    {
    public boolean send(String uniqueId, Date reviewDateStartTime,
            String reviewLocation, int reviewDurationMnts, String from,
            String to, String title, String reviewSubject,
            String reviewDescription, String summary) throws Exception {

        reviewDateStartTime = new Date();       
        String fromMail = from;
        String toMail = to;
        uniqueId = "123456";
        reviewDescription = "testing only";
        reviewSubject = "testing review subject";
        title = "testing";
        summary = "testing summary";
        to="amine.minyawi@gmail.com";
        reviewDurationMnts = 30;
        reviewLocation="test location";
        from=to;
        String meetingStartTime = getReviewTime(reviewDateStartTime,
                reviewDurationMnts, false);
        String meetingEndTime = getReviewTime(reviewDateStartTime,
                reviewDurationMnts, true);

        Properties prop = new Properties();
        StringBuffer sb = new StringBuffer();
        StringBuffer buffer = null;
        Session session = Session.getDefaultInstance(prop, null);
        Message message = new MimeMessage(session);


        try {
            prop.put("mail.smtp.host", "smtp.gmail.com");
            prop.put("mail.smtp.port", "25");

            // Define message
            message.setFrom(new InternetAddress(from));
            message.setRecipients(Message.RecipientType.TO, InternetAddress
                    .parse(to, false));

            message.setSubject(reviewSubject);
            message.setHeader("X-Mailer", "test-Mailer");
            // Create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            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" + "ORGANIZER:MAILTO:" + from + "\n"
                            + "DTSTART:" + meetingStartTime + "\n" + "DTEND:"
                            + meetingEndTime + "\n" + "LOCATION:"
                            + reviewLocation + "\n" + "TRANSP:OPAQUE\n"
                            + "SEQUENCE:0\n" + "UID:" + uniqueId
                            + "@iquest.com\n" + "DTSTAMP:" + meetingEndTime
                            + "\n" + "CATEGORIES:Meeting\n" + "DESCRIPTION:"
                            + reviewDescription + ".\n\n" + "SUMMARY:"
                            + summary + "\n" + "PRIORITY:1\n"
                            + "CLASS:PUBLIC\n" + "BEGIN:VALARM\n"
                            + "TRIGGER:PT1440M\n" + "ACTION:DISPLAY\n"
                            + "DESCRIPTION:Reminder\n" + "END:VALARM\n"
                            + "END:VEVENT\n" + "END:VCALENDAR");
            messageBodyPart.setFileName("TestMeeting.ics");
            messageBodyPart
                    .setDataHandler(new DataHandler(new ByteArrayDataSource(buffer.toString(), "text/iCalendar")));
            messageBodyPart.setHeader("Content-Class",
                    "urn:content-classes:calendarmessage");
            messageBodyPart.setHeader("Content-ID", "calendar_message");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            Transport.send(message);

        }

        catch (Exception me) {
            me.printStackTrace();
        }
        return false;
    }

    public String getReviewTime(Date reviewDateTime, int rDuration, boolean flag) {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat s = new SimpleDateFormat("yyyyMMdd");
        c.setTime(reviewDateTime);
        if (flag == true) {
            c.add(Calendar.MINUTE, rDuration);
        }
        String hour = c.get(Calendar.HOUR_OF_DAY) < 10 ? "0"
                + c.get(Calendar.HOUR_OF_DAY) : ""
                + c.get(Calendar.HOUR_OF_DAY);
        String min = c.get(Calendar.MINUTE) < 10 ? "0" + c.get(Calendar.MINUTE)
                : "" + c.get(Calendar.MINUTE);
        String sec = c.get(Calendar.SECOND) < 10 ? "0" + c.get(Calendar.SECOND)
                : "" + c.get(Calendar.SECOND);

        String date = s.format(new Date(c.getTimeInMillis()));
        String dateTime = date + "T" + hour + min + sec;
        return dateTime;
    }   

    private class ByteArrayDataSource implements DataSource {
        private byte[] data; // data for mail message

        private String type; // content type/mime type

        ByteArrayDataSource(String data, String type) {
            try {
                // Assumption that the string contains only ascii
                // characters ! Else just pass in a charset into this
                // constructor and use it in getBytes()
                this.data = data.getBytes("iso-8859-1");
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.type = type;
        }

        // DataSource interface methods
        public InputStream getInputStream() throws IOException {
            if (data == null)
                throw new IOException(
                        "no data exception in ByteArrayDataSource");
            return new ByteArrayInputStream(data);
        }

        public OutputStream getOutputStream() throws IOException {
            throw new IOException("illegal operation in ByteArrayDataSource");
        }

        public String getContentType() {
            return type;
        }

        public String getName() {
            return "dummy";
        }
    }
public static void main(String args[]) throws Exception
{

SendMailTest s=new SendMailTest();
s.send("01",new Date(), "sale1", 2, "aminee1988@hotmail.com", "someone", "test", "test", "test", "test");

}
}

任何人都帮助我并给我一个解决方案或出了什么问题,谢谢

1 个答案:

答案 0 :(得分:0)

Google似乎使用安全smtp而不是TLS(传输级安全性)。以下RFC描述了该过程,可能会对http://tools.ietf.org/html/rfc3207

提供帮助