简体   繁体   中英

Add IMEI number in android application?

I have to add an IMEI number in this mail body part. I tried on this method, but i can't.

TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId(); 

How can I get and print in body part IMEI number ?

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);
    }
}

You would need to add this in the manifest file

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Then try this -

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getDeviceId();

In emulator, you'll probably get a like a "00000..." value. getDeviceId() returns NULL if device ID is not available.

View this link for reference.

Try this:-

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
   Log.d("Emi::", ">" + telephonyManager.getDeviceId());

with the following permission.

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

telephonyManager.getDeviceId();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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