我必须在此邮件正文部分添加IMEI号码。我尝试过这种方法,但我做不到。
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
如何获取和打印身体部位IMEI号?
public void sendMail(String body, String sender, String recipients, String scilNo)
throws Exception {
try {
File folder = new File(Environment.getExternalStorageDirectory().toString() + "/TEB/Log");
folder.mkdirs();
String extStorageDirectory = folder.toString();
SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy__HH-mm-ss");
now = new Date();
String strDate = sdfDate.format(now);
File file = new File(extStorageDirectory, strDate + ".txt");
file.createNewFile();
String cmd = "logcat -d -v long -f " + file.getAbsolutePath()
+ " *:V";
Runtime.getRuntime().exec(cmd);
DataSource source = new FileDataSource(file);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
StringBuilder sb = new StringBuilder();
sb.append(String.format("%10s: %s\n", "Gönderen", scilNo));
sb.append(String.format("%10s: %s\n", "Mesaj", body ));
DataHandler handler = new DataHandler(new ByteArrayDataSource(sb.toString().getBytes(), "text/plain"));
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
messageBodyPart2.setDataHandler(handler);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(messageBodyPart2);
final MimeMessage message = new MimeMessage(session);
message.setContent(multipart);
message.setSender(new InternetAddress(sender));
message.setSubject("Hata Bildirim Mesajı");
if (recipients.indexOf(',') > 0) {
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipients));
} else {
message.setRecipient(Message.RecipientType.TO,
new InternetAddress(recipients));
}
Thread mySend = new Thread(new Runnable() {
@Override
public void run() {
try {
Transport transport = session.getTransport("smtp");
transport.connect(mailhost, user, password);
transport = session.getTransport("smtp");
Transport.send(message, message.getAllRecipients());
transport.close();
LogUtil.i("Mail SEND DONE");
} catch (MessagingException e) {
LogUtil.e("GMailSender.sendMail.MessagingException", e);
}
}
});
mySend.start();
} catch (Exception e) {
LogUtil.e("GMailSender.sendMail.Exception", e);
}
}
答案 0 :(得分:4)
您需要在清单文件中添加它
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
然后试试这个 -
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();
在模拟器中,您可能会得到类似于&#34; 00000 ......&#34;值。如果设备ID不可用,则getDeviceId()返回NULL。
查看this链接以供参考。
答案 1 :(得分:2)
试试这个: -
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Log.d("Emi::", ">" + telephonyManager.getDeviceId());
获得以下许可。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
答案 2 :(得分:1)
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();