Last active
September 8, 2017 16:46
-
-
Save nomunomu0504/3705feb0635b0f1caae31782e6e8e4b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AsyncHttpRequest extends AsyncTask<String, Void, String> { | |
private Context context; | |
private AsyncHttpRequest(Context context) { | |
this.context = context; | |
} | |
/** | |
* 現在日時をyyyy/MM/dd HH:mm:ss形式で取得する. | |
*/ | |
private static String getNowDate(){ | |
final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); | |
final Date date = new Date(System.currentTimeMillis()); | |
return df.format(date); | |
} | |
@SuppressLint("UnlocalizedSms") @Override | |
protected String doInBackground(String... params) { | |
try { | |
// ----- メール送信 | |
MailSender mailSender = new MailSender(); | |
mailSender.send( | |
params[0], | |
"Hello, " + params[1] + "/r/n" + | |
"From " + params[2] + "/r/n" + | |
"Now Time: " + getNowDate() | |
); | |
} catch (Exception e) { | |
return e.toString(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
Toast.makeText(context, "メールを送信しました", Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MailSender { | |
private Properties properties; | |
public MailSender(){ | |
properties = System.getProperties(); | |
} | |
public void send(String title, String content){ | |
properties.put("mail.smtp.host", "smtp.gmail.com"); | |
properties.put("mail.smtp.port", "587"); | |
properties.put("mail.smtp.auth", "true"); | |
properties.put("mail.smtp.starttls.enable", "true"); | |
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator(){ | |
protected PasswordAuthentication getPasswordAuthentication(){ | |
return new PasswordAuthentication("your_gmail_address@gmail.com", "your_account_password"); // ←修正 | |
} | |
}); | |
MimeMessage message = new MimeMessage(session); | |
try { | |
String from = "xxxxxxxxx@xxxx.xxx"; // ←修正 | |
String[] to = {"yyyyyyyy@yyyy.yyy"}; // ←修正 | |
message.setFrom(new InternetAddress(from)); | |
InternetAddress[] toAddress = new InternetAddress[to.length]; | |
for(int i = 0; i < to.length; i++ ){ | |
toAddress[i] = new InternetAddress(to[i]); | |
} | |
for (InternetAddress toAddres : toAddress) { | |
message.addRecipient(Message.RecipientType.TO, toAddres); | |
} | |
message.setSubject(title); | |
message.setText(content); | |
Transport.send(message); | |
} catch (MessagingException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment