直接从Android应用程序发送电子邮件

时间:2012-11-17 10:50:43

标签: android

我正在创建一个需要将电子邮件发送到特定邮件ID的Android应用程序。我尝试使用intent(ACTION_SEND)。因为它需要用户交互,所以我没有使用这种方法。我想要的是通过后端发送电子邮件(不知道用户)。谁能告诉我最适合的方式?提前谢谢......

4 个答案:

答案 0 :(得分:1)

互联网上有一些关于此的教程,这里有一个 -

答案 1 :(得分:0)

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@test.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Message body");
emailIntent.setType("application/octet-stream");
startActivity(emailIntent);

此外,您可以在服务器端使用HTTP POST并编写特定方法。

答案 2 :(得分:0)

您可以将Java Mail API用于特定的电子邮件ID。你可以解决它。See this link

答案 3 :(得分:0)

使用此课程发送电子邮件。 使用用户名和密码作为参数创建此类的对象。

public class MailSender {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart;
public BodyPart messageBodyPart;
DataSource source;
static {
    Security.addProvider(new com.PackageName.JSSEProvider());
}

public MailSender(String user, String password) {
    this.user = user;
    this.password = password;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.smtp.host", mailhost);
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.quitwait", "false");
    _multipart = new MimeMultipart();
    session = Session.getInstance(props, new MailAuthenticator(user,
            password));

}

public synchronized void sendMail(final String subject, final String uuid,
        final String address, final double latitude,
        final double longitude, final String recipients,
        final String filepath, final Context context, final int i, final int j)
        throws Exception {

    Thread thread = new Thread() {
        public void run() {
            Looper.prepare();
            try {
                System.out.println("SENDING       MAIL");
                Message message = new MimeMessage(session);
                messageBodyPart = new MimeBodyPart();
                DatabaseAdapter db = new DatabaseAdapter(context);
                String eId = db.getYourId();
                message.setFrom(new InternetAddress(eId));
                message.setSubject(subject);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String currentDate = sdf.format(new Date());
                SimpleDateFormat sTf = new SimpleDateFormat("hh:mm:ss");
                String currentTime = sTf.format(new Date());
                System.out.println(i);
                messageBodyPart
                        .setText("Someone tried to unlock your device(ID : "+uuid+" ) at: "
                                + currentTime + " on " + currentDate + "\n"
                                + "Device Location : " + address + "\n"
                                + "Map: " + "http://maps.google.com/?q="
                                + latitude + "," + longitude);
                if (recipients.indexOf(',') > 0)
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse(recipients));
                else
                    message.setRecipient(Message.RecipientType.TO,
                            new InternetAddress(recipients));

                Transport transport = session.getTransport("smtp");
                System.out.println("CONECTING.....");
                transport.connect(mailhost, user, password);
                message.saveChanges();
                message.setContent(_multipart);
                _multipart.addBodyPart(messageBodyPart);
                message.setContent(_multipart);
                if (source != null) {

                    message.setFileName("image");
                }
                Transport.send(message);
                System.out.println("Mail  sent ...");

                }

                transport.close();


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };
    thread.start();

}

public void addAttachment(String filename) throws Exception {
    if (!filename.equals("")) {
        System.out.println("Ataching     file        :)");
        messageBodyPart = new MimeBodyPart();
        source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        _multipart.addBodyPart(messageBodyPart);
        System.out.println("FILE    ATTACHED        :)");
    }
}


class MailAuthenticator extends Authenticator {
    String us;
    String pw;

    public MailAuthenticator(String username, String password) {
        super();
        this.us = username;
        this.pw = password;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(us, pw);
    }
}

}

同时将此内容添加到其他文件中。

public final class JSSEProvider extends Provider {
private static final long serialVersionUID = 1L;

public JSSEProvider() {
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
    AccessController
            .doPrivileged(new java.security.PrivilegedAction<Void>() {
                public Void run() {
                    put("SSLContext.TLS",
                            "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                    put("Alg.Alias.SSLContext.TLSv1", "TLS");
                    put("KeyManagerFactory.X509",
                            "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                    put("TrustManagerFactory.X509",
                            "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                    return null;
                }
            });
}

}