简体   繁体   English

发送电子邮件(Java)

[英]Sending Emails (Java)

The code below opens up Outlook Email as soon as a button is pressed. 按下按钮后,下面的代码会立即打开Outlook电子邮件。 Is there a way to automatically attach a file to the mail as well along with a subject possibly? 有没有办法自动将文件附加到邮件以及可能的主题?

public void onSubmit() {
            try {
                Desktop.getDesktop().browse(new URI("mailto:username@domain.com"));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (URISyntaxException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
    }

I tried changing the Desktop line to this. 我尝试将桌面行更改为此。 Should this work? 这有用吗? Its not compiling though: 它没有编译:

                    Desktop.getDesktop().browse(new URI('mailto:username@domain.com?subject=New_Profile&body=see attachment&attachment="xyz.xml"'));
Desktop desktop = Desktop.getDesktop(); 
    String message = "mailto:username@domain.com?subject=New_Profile&body=seeAttachment&attachment=c:/Update8.txt"; 
    URI uri = URI.create(message); 
    desktop.mail(uri); 

You can't attach anything to the email automatically though, only manually. 但是,您无法手动将任何内容自动附加到电子邮件中。

No, there is no way to attach a file. 不,没有办法附加文件。 You may specify a subject and body. 您可以指定主题和正文。

http://skm.zoomquiet.org/data/20100419224556/index.html http://skm.zoomquiet.org/data/20100419224556/index.html

By the way, you are not sending mail via Java, this way. 顺便说一下,你不是通过Java发送邮件的。 the tags and the question are not about the same topic. 标签和问题不是同一个主题。

You can specify a subject by doing this: 您可以通过执行以下操作指定主题:

Desktop.getDesktop().browse(new URI("mailto:username@domain.com?subject=My+subject"));

Note that subject needs to be URL encoded. 请注意,主题需要进行URL编码。

As far as I know there's no generic way to add attachments, although some mail clients might have a vendor-specific way to do it. 据我所知,没有通用的方法来添加附件,尽管某些邮件客户端可能有特定于供应商的方式来执行此操作。

Short answer is no. 简短的回答是否定的。 Jave does not support attachments via the means, it's been annoying me for 2 years. Jave不通过手段支持附件,这让我烦恼了2年。

The long answer is, you can get it to work using mapi & jni, but be prepared for world of hurt, as not all mail clients are equal 答案很长,你可以使用mapi&jni让它工作,但要为受伤的世界做好准备,因为并非所有邮件客户端都是平等的

Yes you can do. 是的,你可以做到。 My below code works perfect. 我的下面代码非常完美。 Use it 用它

Just call this function to send automated email to client. 只需调用此函数即可向客户端发送自动电子邮件。 In parameter "to" is email address to which you want to send an email. 参数“to”是您要向其发送电子邮件的电子邮件地址。

For attaching pdf reffer https://www.tutorialspoint.com/java/java_sending_email.htm 用于附加pdf reffer https://www.tutorialspoint.com/java/java_sending_email.htm

I usually do it in Maven project. 我通常在Maven项目中这样做。 If you are using maven project then import following dependencies. 如果您正在使用maven项目,则导入以下依赖项。 https://mvnrepository.com/artifact/javax.mail/mail/1.4 https://mvnrepository.com/artifact/javax.mail/mail/1.4

private void sendMail(String to, String subject, String emailBody) throws MessagingException{
    final String username = "youremail@gmail.com";
    final String password = "emailPassword";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("shubham20.yeole@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(emailBody, "text/html; charset=utf-8");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM